blob: b080362a876695df360b11db63106f61c1792194 [file] [log] [blame]
Manuel Stahl9dbf0912013-02-01 08:51:00 +00001/*
2 * itg3200_buffer.c -- support InvenSense ITG3200
3 * Digital 3-Axis Gyroscope driver
4 *
5 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
6 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
7 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/slab.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17
18#include <linux/iio/iio.h>
19#include <linux/iio/buffer.h>
20#include <linux/iio/trigger.h>
21#include <linux/iio/trigger_consumer.h>
22#include <linux/iio/triggered_buffer.h>
23#include <linux/iio/gyro/itg3200.h>
24
25
26static int itg3200_read_all_channels(struct i2c_client *i2c, __be16 *buf)
27{
28 u8 tx = 0x80 | ITG3200_REG_TEMP_OUT_H;
29 struct i2c_msg msg[2] = {
30 {
31 .addr = i2c->addr,
32 .flags = i2c->flags,
33 .len = 1,
34 .buf = &tx,
35 },
36 {
37 .addr = i2c->addr,
38 .flags = i2c->flags | I2C_M_RD,
39 .len = ITG3200_SCAN_ELEMENTS * sizeof(s16),
40 .buf = (char *)&buf,
41 },
42 };
43
44 return i2c_transfer(i2c->adapter, msg, 2);
45}
46
47static irqreturn_t itg3200_trigger_handler(int irq, void *p)
48{
49 struct iio_poll_func *pf = p;
50 struct iio_dev *indio_dev = pf->indio_dev;
51 struct itg3200 *st = iio_priv(indio_dev);
Jonathan Cameronc7975bd2020-07-22 16:50:41 +010052 /*
53 * Ensure correct alignment and padding including for the
54 * timestamp that may be inserted.
55 */
56 struct {
57 __be16 buf[ITG3200_SCAN_ELEMENTS];
58 s64 ts __aligned(8);
59 } scan;
Manuel Stahl9dbf0912013-02-01 08:51:00 +000060
Jonathan Cameronc7975bd2020-07-22 16:50:41 +010061 int ret = itg3200_read_all_channels(st->i2c, scan.buf);
Manuel Stahl9dbf0912013-02-01 08:51:00 +000062 if (ret < 0)
63 goto error_ret;
64
Jonathan Cameronc7975bd2020-07-22 16:50:41 +010065 iio_push_to_buffers_with_timestamp(indio_dev, &scan, pf->timestamp);
Manuel Stahl9dbf0912013-02-01 08:51:00 +000066
Manuel Stahl9dbf0912013-02-01 08:51:00 +000067 iio_trigger_notify_done(indio_dev->trig);
68
69error_ret:
70 return IRQ_HANDLED;
71}
72
73int itg3200_buffer_configure(struct iio_dev *indio_dev)
74{
75 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
76 itg3200_trigger_handler, NULL);
77}
78
79void itg3200_buffer_unconfigure(struct iio_dev *indio_dev)
80{
81 iio_triggered_buffer_cleanup(indio_dev);
82}
83
84
85static int itg3200_data_rdy_trigger_set_state(struct iio_trigger *trig,
86 bool state)
87{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +000088 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
Manuel Stahl9dbf0912013-02-01 08:51:00 +000089 int ret;
90 u8 msc;
91
92 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, &msc);
93 if (ret)
94 goto error_ret;
95
96 if (state)
97 msc |= ITG3200_IRQ_DATA_RDY_ENABLE;
98 else
99 msc &= ~ITG3200_IRQ_DATA_RDY_ENABLE;
100
101 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, msc);
102 if (ret)
103 goto error_ret;
104
105error_ret:
106 return ret;
107
108}
109
110static const struct iio_trigger_ops itg3200_trigger_ops = {
Manuel Stahl9dbf0912013-02-01 08:51:00 +0000111 .set_trigger_state = &itg3200_data_rdy_trigger_set_state,
112};
113
114int itg3200_probe_trigger(struct iio_dev *indio_dev)
115{
116 int ret;
117 struct itg3200 *st = iio_priv(indio_dev);
118
119 st->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
120 indio_dev->id);
121 if (!st->trig)
122 return -ENOMEM;
123
124 ret = request_irq(st->i2c->irq,
125 &iio_trigger_generic_data_rdy_poll,
126 IRQF_TRIGGER_RISING,
127 "itg3200_data_rdy",
128 st->trig);
129 if (ret)
130 goto error_free_trig;
131
132
133 st->trig->dev.parent = &st->i2c->dev;
134 st->trig->ops = &itg3200_trigger_ops;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000135 iio_trigger_set_drvdata(st->trig, indio_dev);
Manuel Stahl9dbf0912013-02-01 08:51:00 +0000136 ret = iio_trigger_register(st->trig);
137 if (ret)
138 goto error_free_irq;
139
140 /* select default trigger */
Srinivas Pandruvada0b4dce22014-08-22 21:48:00 +0100141 indio_dev->trig = iio_trigger_get(st->trig);
Manuel Stahl9dbf0912013-02-01 08:51:00 +0000142
143 return 0;
144
145error_free_irq:
146 free_irq(st->i2c->irq, st->trig);
147error_free_trig:
148 iio_trigger_free(st->trig);
149 return ret;
150}
151
152void itg3200_remove_trigger(struct iio_dev *indio_dev)
153{
154 struct itg3200 *st = iio_priv(indio_dev);
155
156 iio_trigger_unregister(st->trig);
157 free_irq(st->i2c->irq, st->trig);
158 iio_trigger_free(st->trig);
159}