blob: a87fbe8b0ee9700328b2f6c0e9577d6b5830761d [file] [log] [blame]
Jonathan Cameron0a1231d2009-08-18 18:06:29 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010013#include <linux/kernel.h>
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010014#include <linux/i2c.h>
Jonathan Cameron82020b02010-05-04 14:43:03 +010015#include <linux/bitops.h>
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010016
17#include "../iio.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010018#include "../buffer.h"
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010019#include "../ring_sw.h"
Jonathan Cameron3f723952011-08-24 17:28:39 +010020#include "../trigger_consumer.h"
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010021
22#include "max1363.h"
23
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010024int max1363_single_channel_from_ring(const long *mask, struct max1363_state *st)
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010025{
Jonathan Cameron14555b12011-09-21 11:15:57 +010026 struct iio_buffer *ring = iio_priv_to_dev(st)->buffer;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010027 int count = 0, ret, index;
Jonathan Cameron82020b02010-05-04 14:43:03 +010028 u8 *ring_data;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010029 index = find_first_bit(mask, MAX1363_MAX_CHANNELS);
30
31 if (!(test_bit(index, st->current_mode->modemask))) {
Jonathan Cameron82020b02010-05-04 14:43:03 +010032 ret = -EBUSY;
33 goto error_ret;
34 }
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010035
Jonathan Cameron5565a452011-05-18 14:42:24 +010036 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
37 GFP_KERNEL);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010038 if (ring_data == NULL) {
39 ret = -ENOMEM;
40 goto error_ret;
41 }
Jonathan Cameron5565a452011-05-18 14:42:24 +010042 ret = ring->access->read_last(ring, ring_data);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010043 if (ret)
44 goto error_free_ring_data;
Jonathan Cameron82020b02010-05-04 14:43:03 +010045 /* Need a count of channels prior to this one */
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010046
47 count = bitmap_weight(mask, index - 1);
Jonathan Cameron3bf877c2010-05-04 14:43:10 +010048 if (st->chip_info->bits != 8)
Jonathan Cameron81b77f92010-05-16 21:29:25 +010049 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
Jonathan Cameron3bf877c2010-05-04 14:43:10 +010050 + (int)(ring_data[count*2 + 1]);
51 else
Jonathan Cameron81b77f92010-05-16 21:29:25 +010052 ret = ring_data[count];
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010053
54error_free_ring_data:
55 kfree(ring_data);
56error_ret:
57 return ret;
58}
59
Jonathan Cameron58f0a252011-05-18 14:42:38 +010060
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010061/**
Jonathan Cameronc40ab872010-06-26 12:54:14 +010062 * max1363_ring_preenable() - setup the parameters of the ring before enabling
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010063 *
64 * The complex nature of the setting of the nuber of bytes per datum is due
65 * to this driver currently ensuring that the timestamp is stored at an 8
66 * byte boundary.
67 **/
68static int max1363_ring_preenable(struct iio_dev *indio_dev)
69{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +010070 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +010071 struct iio_buffer *ring = indio_dev->buffer;
Jonathan Cameron9cc55982011-04-15 18:55:57 +010072 size_t d_size = 0;
Jonathan Cameron82020b02010-05-04 14:43:03 +010073 unsigned long numvals;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010074
Jonathan Cameron82020b02010-05-04 14:43:03 +010075 /*
76 * Need to figure out the current mode based upon the requested
77 * scan mask in iio_dev
78 */
Manuel Stahlbf329632010-08-31 11:32:52 +020079 st->current_mode = max1363_match_mode(ring->scan_mask,
Jonathan Cameron82020b02010-05-04 14:43:03 +010080 st->chip_info);
81 if (!st->current_mode)
82 return -EINVAL;
83
84 max1363_set_scan_mode(st);
85
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010086 numvals = bitmap_weight(st->current_mode->modemask,
87 indio_dev->masklength);
Jonathan Cameron5565a452011-05-18 14:42:24 +010088 if (ring->access->set_bytes_per_datum) {
Jonathan Cameron9cc55982011-04-15 18:55:57 +010089 if (ring->scan_timestamp)
90 d_size += sizeof(s64);
Jonathan Cameron3bf877c2010-05-04 14:43:10 +010091 if (st->chip_info->bits != 8)
Jonathan Cameron9cc55982011-04-15 18:55:57 +010092 d_size += numvals*2;
Jonathan Cameron3bf877c2010-05-04 14:43:10 +010093 else
Jonathan Cameron9cc55982011-04-15 18:55:57 +010094 d_size += numvals;
95 if (ring->scan_timestamp && (d_size % 8))
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010096 d_size += 8 - (d_size % 8);
Jonathan Cameron5565a452011-05-18 14:42:24 +010097 ring->access->set_bytes_per_datum(ring, d_size);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +010098 }
99
100 return 0;
101}
102
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100103static irqreturn_t max1363_trigger_handler(int irq, void *p)
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100104{
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100105 struct iio_poll_func *pf = p;
Jonathan Camerone65bc6a2011-08-24 17:28:36 +0100106 struct iio_dev *indio_dev = pf->indio_dev;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100107 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100108 s64 time_ns;
109 __u8 *rxbuf;
110 int b_sent;
111 size_t d_size;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100112 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
113 MAX1363_MAX_CHANNELS);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100114
115 /* Ensure the timestamp is 8 byte aligned */
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100116 if (st->chip_info->bits != 8)
117 d_size = numvals*2 + sizeof(s64);
118 else
119 d_size = numvals + sizeof(s64);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100120 if (d_size % sizeof(s64))
121 d_size += sizeof(s64) - (d_size % sizeof(s64));
122
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100123 /* Monitor mode prevents reading. Whilst not currently implemented
124 * might as well have this test in here in the meantime as it does
125 * no harm.
126 */
Jonathan Cameron82020b02010-05-04 14:43:03 +0100127 if (numvals == 0)
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100128 return IRQ_HANDLED;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100129
130 rxbuf = kmalloc(d_size, GFP_KERNEL);
131 if (rxbuf == NULL)
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100132 return -ENOMEM;
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100133 if (st->chip_info->bits != 8)
134 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
135 else
136 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100137 if (b_sent < 0)
138 goto done;
139
140 time_ns = iio_get_time_ns();
141
142 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
143
Jonathan Cameron14555b12011-09-21 11:15:57 +0100144 indio_dev->buffer->access->store_to(indio_dev->buffer, rxbuf, time_ns);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100145done:
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100146 iio_trigger_notify_done(indio_dev->trig);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100147 kfree(rxbuf);
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100148
149 return IRQ_HANDLED;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100150}
151
Jonathan Cameron14555b12011-09-21 11:15:57 +0100152static const struct iio_buffer_setup_ops max1363_ring_setup_ops = {
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100153 .postenable = &iio_triggered_buffer_postenable,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100154 .preenable = &max1363_ring_preenable,
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100155 .predisable = &iio_triggered_buffer_predisable,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100156};
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100157
158int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
159{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100160 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100161 int ret = 0;
162
Jonathan Cameron14555b12011-09-21 11:15:57 +0100163 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
164 if (!indio_dev->buffer) {
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100165 ret = -ENOMEM;
166 goto error_ret;
167 }
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100168 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
169 &max1363_trigger_handler,
170 IRQF_ONESHOT,
171 indio_dev,
172 "%s_consumer%d",
173 st->client->name,
174 indio_dev->id);
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100175 if (indio_dev->pollfunc == NULL) {
176 ret = -ENOMEM;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100177 goto error_deallocate_sw_rb;
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100178 }
Jonathan Cameron5565a452011-05-18 14:42:24 +0100179 /* Effectively select the ring buffer implementation */
Jonathan Cameron14555b12011-09-21 11:15:57 +0100180 indio_dev->buffer->access = &ring_sw_access_funcs;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100181 /* Ring buffer functions - here trigger setup related */
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 indio_dev->buffer->setup_ops = &max1363_ring_setup_ops;
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100183
184 /* Flag that polled ring buffering is possible */
Jonathan Cameronec3afa42011-09-21 11:15:54 +0100185 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
Jonathan Cameron6da288a2011-05-18 14:41:23 +0100186
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100187 return 0;
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100188
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100189error_deallocate_sw_rb:
Jonathan Cameron14555b12011-09-21 11:15:57 +0100190 iio_sw_rb_free(indio_dev->buffer);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100191error_ret:
192 return ret;
193}
194
195void max1363_ring_cleanup(struct iio_dev *indio_dev)
196{
197 /* ensure that the trigger has been detached */
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100198 iio_dealloc_pollfunc(indio_dev->pollfunc);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100199 iio_sw_rb_free(indio_dev->buffer);
Jonathan Cameron0a1231d2009-08-18 18:06:29 +0100200}