1 // SPDX-License-Identifier: GPL-2.0-only
3 * itg3200_buffer.c -- support InvenSense ITG3200
4 * Digital 3-Axis Gyroscope driver
6 * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
7 * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/gyro/itg3200.h>
23 static int itg3200_read_all_channels(struct i2c_client
*i2c
, __be16
*buf
)
25 u8 tx
= 0x80 | ITG3200_REG_TEMP_OUT_H
;
26 struct i2c_msg msg
[2] = {
35 .flags
= i2c
->flags
| I2C_M_RD
,
36 .len
= ITG3200_SCAN_ELEMENTS
* sizeof(s16
),
41 return i2c_transfer(i2c
->adapter
, msg
, 2);
44 static irqreturn_t
itg3200_trigger_handler(int irq
, void *p
)
46 struct iio_poll_func
*pf
= p
;
47 struct iio_dev
*indio_dev
= pf
->indio_dev
;
48 struct itg3200
*st
= iio_priv(indio_dev
);
49 __be16 buf
[ITG3200_SCAN_ELEMENTS
+ sizeof(s64
)/sizeof(u16
)];
51 int ret
= itg3200_read_all_channels(st
->i2c
, buf
);
55 iio_push_to_buffers_with_timestamp(indio_dev
, buf
, pf
->timestamp
);
57 iio_trigger_notify_done(indio_dev
->trig
);
63 int itg3200_buffer_configure(struct iio_dev
*indio_dev
)
65 return iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
66 itg3200_trigger_handler
, NULL
);
69 void itg3200_buffer_unconfigure(struct iio_dev
*indio_dev
)
71 iio_triggered_buffer_cleanup(indio_dev
);
75 static int itg3200_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
78 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
82 ret
= itg3200_read_reg_8(indio_dev
, ITG3200_REG_IRQ_CONFIG
, &msc
);
87 msc
|= ITG3200_IRQ_DATA_RDY_ENABLE
;
89 msc
&= ~ITG3200_IRQ_DATA_RDY_ENABLE
;
91 ret
= itg3200_write_reg_8(indio_dev
, ITG3200_REG_IRQ_CONFIG
, msc
);
100 static const struct iio_trigger_ops itg3200_trigger_ops
= {
101 .set_trigger_state
= &itg3200_data_rdy_trigger_set_state
,
104 int itg3200_probe_trigger(struct iio_dev
*indio_dev
)
107 struct itg3200
*st
= iio_priv(indio_dev
);
109 st
->trig
= iio_trigger_alloc("%s-dev%d", indio_dev
->name
,
114 ret
= request_irq(st
->i2c
->irq
,
115 &iio_trigger_generic_data_rdy_poll
,
120 goto error_free_trig
;
123 st
->trig
->dev
.parent
= &st
->i2c
->dev
;
124 st
->trig
->ops
= &itg3200_trigger_ops
;
125 iio_trigger_set_drvdata(st
->trig
, indio_dev
);
126 ret
= iio_trigger_register(st
->trig
);
130 /* select default trigger */
131 indio_dev
->trig
= iio_trigger_get(st
->trig
);
136 free_irq(st
->i2c
->irq
, st
->trig
);
138 iio_trigger_free(st
->trig
);
142 void itg3200_remove_trigger(struct iio_dev
*indio_dev
)
144 struct itg3200
*st
= iio_priv(indio_dev
);
146 iio_trigger_unregister(st
->trig
);
147 free_irq(st
->i2c
->irq
, st
->trig
);
148 iio_trigger_free(st
->trig
);