2 * Copyright (C) 2012 Invensense, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 * @brief Hardware drivers.
20 * @file inv_yas53x_ring.c
21 * @brief Invensense implementation for yas530/yas532/yas533.
22 * @details This driver currently works for the yas530/yas532/yas533.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/err.h>
30 #include <linux/delay.h>
31 #include <linux/sysfs.h>
32 #include <linux/jiffies.h>
33 #include <linux/irq.h>
34 #include <linux/interrupt.h>
35 #include <linux/kfifo.h>
36 #include <linux/poll.h>
37 #include <linux/miscdevice.h>
40 #include "kfifo_buf.h"
41 #include "trigger_consumer.h"
44 #include "inv_yas53x_iio.h"
46 static s64 get_time_ns(void)
51 return timespec_to_ns(&ts);
54 static int put_scan_to_buf(struct iio_dev *indio_dev, unsigned char *d,
55 short *s, int scan_index)
57 struct iio_buffer *ring = indio_dev->buffer;
62 for (i = 0; i < 3; i++) {
63 st = iio_scan_mask_query(indio_dev, ring, scan_index + i);
65 memcpy(&d[d_ind], &s[i], sizeof(s[i]));
66 d_ind += sizeof(s[i]);
74 * inv_read_yas53x_fifo() - Transfer data from FIFO to ring buffer.
76 void inv_read_yas53x_fifo(struct iio_dev *indio_dev)
78 struct inv_compass_state *st = iio_priv(indio_dev);
79 struct iio_buffer *ring = indio_dev->buffer;
85 if (!yas53x_read(st, st->compass_data, &overunderflow)) {
87 d_ind = put_scan_to_buf(indio_dev, tmp, st->compass_data,
88 INV_YAS53X_SCAN_MAGN_X);
89 if (ring->scan_timestamp)
90 tmp_buf[(d_ind + 7) / 8] = get_time_ns();
91 ring->access->store_to(indio_dev->buffer, tmp, 0);
95 if (!st->overunderflow)
96 st->overunderflow = 1;
101 void inv_yas53x_unconfigure_ring(struct iio_dev *indio_dev)
103 iio_kfifo_free(indio_dev->buffer);
106 static int inv_yas53x_postenable(struct iio_dev *indio_dev)
108 struct inv_compass_state *st = iio_priv(indio_dev);
109 struct iio_buffer *ring = indio_dev->buffer;
111 /* when all the outputs are disabled, even though buffer/enable is on,
113 if (!(iio_scan_mask_query(indio_dev, ring, INV_YAS53X_SCAN_MAGN_X) ||
114 iio_scan_mask_query(indio_dev, ring, INV_YAS53X_SCAN_MAGN_Y) ||
115 iio_scan_mask_query(indio_dev, ring, INV_YAS53X_SCAN_MAGN_Z)))
118 set_yas53x_enable(indio_dev, true);
119 schedule_delayed_work(&st->work,
120 msecs_to_jiffies(st->delay));
125 static int inv_yas53x_predisable(struct iio_dev *indio_dev)
127 struct inv_compass_state *st = iio_priv(indio_dev);
128 struct iio_buffer *ring = indio_dev->buffer;
130 cancel_delayed_work_sync(&st->work);
131 clear_bit(INV_YAS53X_SCAN_MAGN_X, ring->scan_mask);
132 clear_bit(INV_YAS53X_SCAN_MAGN_Y, ring->scan_mask);
133 clear_bit(INV_YAS53X_SCAN_MAGN_Z, ring->scan_mask);
138 static const struct iio_buffer_setup_ops inv_yas53x_ring_setup_ops = {
139 .preenable = &iio_sw_buffer_preenable,
140 .postenable = &inv_yas53x_postenable,
141 .predisable = &inv_yas53x_predisable,
144 int inv_yas53x_configure_ring(struct iio_dev *indio_dev)
147 struct iio_buffer *ring;
149 ring = iio_kfifo_allocate(indio_dev);
154 indio_dev->buffer = ring;
155 /* setup ring buffer */
156 ring->scan_timestamp = true;
157 indio_dev->setup_ops = &inv_yas53x_ring_setup_ops;
159 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;