aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/gyro
diff options
context:
space:
mode:
authorManuel Stahl <manuel.stahl@iis.fraunhofer.de>2013-02-01 03:51:00 -0500
committerJonathan Cameron <jic23@kernel.org>2013-02-02 04:33:33 -0500
commit9dbf091da080508e9f632d307f357beb79a0766b (patch)
tree4f3fe19f392e7b55b2c349cb6df65155ad98d0e8 /drivers/iio/gyro
parent420b0fcb523b61eefb347317d3956db3249e968d (diff)
iio: gyro: Add itg3200
This patch adds support for the InvenSense itg3200. The itg3200 is a three-axis gyro with 16-bit ADC and I2C interface. Signed-off-by: Manuel Stahl <manuel.stahl@iis.fraunhofer.de> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/gyro')
-rw-r--r--drivers/iio/gyro/Kconfig8
-rw-r--r--drivers/iio/gyro/Makefile4
-rw-r--r--drivers/iio/gyro/itg3200_buffer.c156
-rw-r--r--drivers/iio/gyro/itg3200_core.c401
4 files changed, 569 insertions, 0 deletions
diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig
index 51ac6bc791c6..6be4628faffe 100644
--- a/drivers/iio/gyro/Kconfig
+++ b/drivers/iio/gyro/Kconfig
@@ -70,4 +70,12 @@ config IIO_ST_GYRO_SPI_3AXIS
70 depends on IIO_ST_GYRO_3AXIS 70 depends on IIO_ST_GYRO_3AXIS
71 depends on IIO_ST_SENSORS_SPI 71 depends on IIO_ST_SENSORS_SPI
72 72
73config ITG3200
74 tristate "InvenSense ITG3200 Digital 3-Axis Gyroscope I2C driver"
75 depends on I2C
76 select IIO_TRIGGERED_BUFFER if IIO_BUFFER
77 help
78 Say yes here to add support for the InvenSense ITG3200 digital
79 3-axis gyroscope sensor.
80
73endmenu 81endmenu
diff --git a/drivers/iio/gyro/Makefile b/drivers/iio/gyro/Makefile
index eff9bb0119a2..225d289082e6 100644
--- a/drivers/iio/gyro/Makefile
+++ b/drivers/iio/gyro/Makefile
@@ -8,6 +8,10 @@ obj-$(CONFIG_ADXRS450) += adxrs450.o
8 8
9obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o 9obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o
10 10
11itg3200-y := itg3200_core.o
12itg3200-$(CONFIG_IIO_BUFFER) += itg3200_buffer.o
13obj-$(CONFIG_ITG3200) += itg3200.o
14
11obj-$(CONFIG_IIO_ST_GYRO_3AXIS) += st_gyro.o 15obj-$(CONFIG_IIO_ST_GYRO_3AXIS) += st_gyro.o
12st_gyro-y := st_gyro_core.o 16st_gyro-y := st_gyro_core.o
13st_gyro-$(CONFIG_IIO_BUFFER) += st_gyro_buffer.o 17st_gyro-$(CONFIG_IIO_BUFFER) += st_gyro_buffer.o
diff --git a/drivers/iio/gyro/itg3200_buffer.c b/drivers/iio/gyro/itg3200_buffer.c
new file mode 100644
index 000000000000..f667d2c8c00f
--- /dev/null
+++ b/drivers/iio/gyro/itg3200_buffer.c
@@ -0,0 +1,156 @@
1/*
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);
52 __be16 buf[ITG3200_SCAN_ELEMENTS + sizeof(s64)/sizeof(u16)];
53
54 int ret = itg3200_read_all_channels(st->i2c, buf);
55 if (ret < 0)
56 goto error_ret;
57
58 if (indio_dev->scan_timestamp)
59 memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
60 &pf->timestamp, sizeof(pf->timestamp));
61
62 iio_push_to_buffers(indio_dev, (u8 *)buf);
63 iio_trigger_notify_done(indio_dev->trig);
64
65error_ret:
66 return IRQ_HANDLED;
67}
68
69int itg3200_buffer_configure(struct iio_dev *indio_dev)
70{
71 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
72 itg3200_trigger_handler, NULL);
73}
74
75void itg3200_buffer_unconfigure(struct iio_dev *indio_dev)
76{
77 iio_triggered_buffer_cleanup(indio_dev);
78}
79
80
81static int itg3200_data_rdy_trigger_set_state(struct iio_trigger *trig,
82 bool state)
83{
84 struct iio_dev *indio_dev = trig->private_data;
85 int ret;
86 u8 msc;
87
88 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, &msc);
89 if (ret)
90 goto error_ret;
91
92 if (state)
93 msc |= ITG3200_IRQ_DATA_RDY_ENABLE;
94 else
95 msc &= ~ITG3200_IRQ_DATA_RDY_ENABLE;
96
97 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, msc);
98 if (ret)
99 goto error_ret;
100
101error_ret:
102 return ret;
103
104}
105
106static const struct iio_trigger_ops itg3200_trigger_ops = {
107 .owner = THIS_MODULE,
108 .set_trigger_state = &itg3200_data_rdy_trigger_set_state,
109};
110
111int itg3200_probe_trigger(struct iio_dev *indio_dev)
112{
113 int ret;
114 struct itg3200 *st = iio_priv(indio_dev);
115
116 st->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
117 indio_dev->id);
118 if (!st->trig)
119 return -ENOMEM;
120
121 ret = request_irq(st->i2c->irq,
122 &iio_trigger_generic_data_rdy_poll,
123 IRQF_TRIGGER_RISING,
124 "itg3200_data_rdy",
125 st->trig);
126 if (ret)
127 goto error_free_trig;
128
129
130 st->trig->dev.parent = &st->i2c->dev;
131 st->trig->ops = &itg3200_trigger_ops;
132 st->trig->private_data = indio_dev;
133 ret = iio_trigger_register(st->trig);
134 if (ret)
135 goto error_free_irq;
136
137 /* select default trigger */
138 indio_dev->trig = st->trig;
139
140 return 0;
141
142error_free_irq:
143 free_irq(st->i2c->irq, st->trig);
144error_free_trig:
145 iio_trigger_free(st->trig);
146 return ret;
147}
148
149void itg3200_remove_trigger(struct iio_dev *indio_dev)
150{
151 struct itg3200 *st = iio_priv(indio_dev);
152
153 iio_trigger_unregister(st->trig);
154 free_irq(st->i2c->irq, st->trig);
155 iio_trigger_free(st->trig);
156}
diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
new file mode 100644
index 000000000000..df2e6aa5d73b
--- /dev/null
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -0,0 +1,401 @@
1/*
2 * itg3200_core.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 * TODO:
14 * - Support digital low pass filter
15 * - Support power management
16 */
17
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/i2c.h>
21#include <linux/gpio.h>
22#include <linux/slab.h>
23#include <linux/stat.h>
24#include <linux/module.h>
25#include <linux/delay.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/events.h>
30#include <linux/iio/buffer.h>
31
32#include <linux/iio/gyro/itg3200.h>
33
34
35int itg3200_write_reg_8(struct iio_dev *indio_dev,
36 u8 reg_address, u8 val)
37{
38 struct itg3200 *st = iio_priv(indio_dev);
39
40 return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
41}
42
43int itg3200_read_reg_8(struct iio_dev *indio_dev,
44 u8 reg_address, u8 *val)
45{
46 struct itg3200 *st = iio_priv(indio_dev);
47 int ret;
48
49 ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
50 if (ret < 0)
51 return ret;
52 *val = ret;
53 return 0;
54}
55
56static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
57 int *val)
58{
59 struct itg3200 *st = iio_priv(indio_dev);
60 struct i2c_client *client = st->i2c;
61 int ret;
62 s16 out;
63
64 struct i2c_msg msg[2] = {
65 {
66 .addr = client->addr,
67 .flags = client->flags,
68 .len = 1,
69 .buf = (char *)&lower_reg_address,
70 },
71 {
72 .addr = client->addr,
73 .flags = client->flags | I2C_M_RD,
74 .len = 2,
75 .buf = (char *)&out,
76 },
77 };
78
79 lower_reg_address |= 0x80;
80 ret = i2c_transfer(client->adapter, msg, 2);
81 be16_to_cpus(&out);
82 *val = out;
83
84 return (ret == 2) ? 0 : ret;
85}
86
87static int itg3200_read_raw(struct iio_dev *indio_dev,
88 const struct iio_chan_spec *chan,
89 int *val, int *val2, long info)
90{
91 int ret = 0;
92 u8 reg;
93
94 switch (info) {
95 case IIO_CHAN_INFO_RAW:
96 reg = (u8)chan->address;
97 ret = itg3200_read_reg_s16(indio_dev, reg, val);
98 return IIO_VAL_INT;
99 case IIO_CHAN_INFO_SCALE:
100 *val = 0;
101 if (chan->type == IIO_TEMP)
102 *val2 = 1000000000/280;
103 else
104 *val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
105 return IIO_VAL_INT_PLUS_NANO;
106 case IIO_CHAN_INFO_OFFSET:
107 /* Only the temperature channel has an offset */
108 *val = 23000;
109 return IIO_VAL_INT;
110 default:
111 return -EINVAL;
112 }
113
114 return ret;
115}
116
117static ssize_t itg3200_read_frequency(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
120 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
121 int ret, sps;
122 u8 val;
123
124 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
125 if (ret)
126 return ret;
127
128 sps = (val & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
129
130 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, &val);
131 if (ret)
132 return ret;
133
134 sps /= val + 1;
135
136 return sprintf(buf, "%d\n", sps);
137}
138
139static ssize_t itg3200_write_frequency(struct device *dev,
140 struct device_attribute *attr,
141 const char *buf,
142 size_t len)
143{
144 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
145 unsigned val;
146 int ret;
147 u8 t;
148
149 ret = kstrtouint(buf, 10, &val);
150 if (ret)
151 return ret;
152
153 mutex_lock(&indio_dev->mlock);
154
155 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
156 if (ret)
157 goto err_ret;
158
159 if (val == 0) {
160 ret = -EINVAL;
161 goto err_ret;
162 }
163 t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
164
165 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, t);
166
167err_ret:
168 mutex_unlock(&indio_dev->mlock);
169
170 return ret ? ret : len;
171}
172
173/*
174 * Reset device and internal registers to the power-up-default settings
175 * Use the gyro clock as reference, as suggested by the datasheet
176 */
177static int itg3200_reset(struct iio_dev *indio_dev)
178{
179 struct itg3200 *st = iio_priv(indio_dev);
180 int ret;
181
182 dev_dbg(&st->i2c->dev, "reset device");
183
184 ret = itg3200_write_reg_8(indio_dev,
185 ITG3200_REG_POWER_MANAGEMENT,
186 ITG3200_RESET);
187 if (ret) {
188 dev_err(&st->i2c->dev, "error resetting device");
189 goto error_ret;
190 }
191
192 /* Wait for PLL (1ms according to datasheet) */
193 udelay(1500);
194
195 ret = itg3200_write_reg_8(indio_dev,
196 ITG3200_REG_IRQ_CONFIG,
197 ITG3200_IRQ_ACTIVE_HIGH |
198 ITG3200_IRQ_PUSH_PULL |
199 ITG3200_IRQ_LATCH_50US_PULSE |
200 ITG3200_IRQ_LATCH_CLEAR_ANY);
201
202 if (ret)
203 dev_err(&st->i2c->dev, "error init device");
204
205error_ret:
206 return ret;
207}
208
209/* itg3200_enable_full_scale() - Disables the digital low pass filter */
210static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
211{
212 u8 val;
213 int ret;
214
215 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
216 if (ret)
217 goto err_ret;
218
219 val |= ITG3200_DLPF_FS_SEL_2000;
220 return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
221
222err_ret:
223 return ret;
224}
225
226static int itg3200_initial_setup(struct iio_dev *indio_dev)
227{
228 struct itg3200 *st = iio_priv(indio_dev);
229 int ret;
230 u8 val;
231
232 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
233 if (ret)
234 goto err_ret;
235
236 if (((val >> 1) & 0x3f) != 0x34) {
237 dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
238 ret = -ENXIO;
239 goto err_ret;
240 }
241
242 ret = itg3200_reset(indio_dev);
243 if (ret)
244 goto err_ret;
245
246 ret = itg3200_enable_full_scale(indio_dev);
247err_ret:
248 return ret;
249}
250
251#define ITG3200_TEMP_INFO_MASK (IIO_CHAN_INFO_OFFSET_SHARED_BIT | \
252 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
253 IIO_CHAN_INFO_RAW_SEPARATE_BIT)
254#define ITG3200_GYRO_INFO_MASK (IIO_CHAN_INFO_SCALE_SHARED_BIT | \
255 IIO_CHAN_INFO_RAW_SEPARATE_BIT)
256
257#define ITG3200_ST \
258 { .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
259
260#define ITG3200_GYRO_CHAN(_mod) { \
261 .type = IIO_ANGL_VEL, \
262 .modified = 1, \
263 .channel2 = IIO_MOD_ ## _mod, \
264 .info_mask = ITG3200_GYRO_INFO_MASK, \
265 .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
266 .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
267 .scan_type = ITG3200_ST, \
268}
269
270static const struct iio_chan_spec itg3200_channels[] = {
271 {
272 .type = IIO_TEMP,
273 .channel2 = IIO_NO_MOD,
274 .info_mask = ITG3200_TEMP_INFO_MASK,
275 .address = ITG3200_REG_TEMP_OUT_H,
276 .scan_index = ITG3200_SCAN_TEMP,
277 .scan_type = ITG3200_ST,
278 },
279 ITG3200_GYRO_CHAN(X),
280 ITG3200_GYRO_CHAN(Y),
281 ITG3200_GYRO_CHAN(Z),
282 IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
283};
284
285/* IIO device attributes */
286static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, itg3200_read_frequency,
287 itg3200_write_frequency);
288
289static struct attribute *itg3200_attributes[] = {
290 &iio_dev_attr_sampling_frequency.dev_attr.attr,
291 NULL
292};
293
294static const struct attribute_group itg3200_attribute_group = {
295 .attrs = itg3200_attributes,
296};
297
298static const struct iio_info itg3200_info = {
299 .attrs = &itg3200_attribute_group,
300 .read_raw = &itg3200_read_raw,
301 .driver_module = THIS_MODULE,
302};
303
304static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
305
306static int itg3200_probe(struct i2c_client *client,
307 const struct i2c_device_id *id)
308{
309 int ret;
310 struct itg3200 *st;
311 struct iio_dev *indio_dev;
312
313 dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
314
315 indio_dev = iio_device_alloc(sizeof(*st));
316 if (indio_dev == NULL) {
317 ret = -ENOMEM;
318 goto error_ret;
319 }
320
321 st = iio_priv(indio_dev);
322
323 i2c_set_clientdata(client, indio_dev);
324 st->i2c = client;
325
326 indio_dev->dev.parent = &client->dev;
327 indio_dev->name = client->dev.driver->name;
328 indio_dev->channels = itg3200_channels;
329 indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
330 indio_dev->available_scan_masks = itg3200_available_scan_masks;
331 indio_dev->info = &itg3200_info;
332 indio_dev->modes = INDIO_DIRECT_MODE;
333
334 ret = itg3200_buffer_configure(indio_dev);
335 if (ret)
336 goto error_free_dev;
337
338 if (client->irq) {
339 ret = itg3200_probe_trigger(indio_dev);
340 if (ret)
341 goto error_unconfigure_buffer;
342 }
343
344 ret = itg3200_initial_setup(indio_dev);
345 if (ret)
346 goto error_remove_trigger;
347
348 ret = iio_device_register(indio_dev);
349 if (ret)
350 goto error_remove_trigger;
351
352 return 0;
353
354error_remove_trigger:
355 if (client->irq)
356 itg3200_remove_trigger(indio_dev);
357error_unconfigure_buffer:
358 itg3200_buffer_unconfigure(indio_dev);
359error_free_dev:
360 iio_device_free(indio_dev);
361error_ret:
362 return ret;
363}
364
365static int itg3200_remove(struct i2c_client *client)
366{
367 struct iio_dev *indio_dev = i2c_get_clientdata(client);
368
369 iio_device_unregister(indio_dev);
370
371 if (client->irq)
372 itg3200_remove_trigger(indio_dev);
373
374 itg3200_buffer_unconfigure(indio_dev);
375
376 iio_device_free(indio_dev);
377
378 return 0;
379}
380
381static const struct i2c_device_id itg3200_id[] = {
382 { "itg3200", 0 },
383 { }
384};
385MODULE_DEVICE_TABLE(i2c, itg3200_id);
386
387static struct i2c_driver itg3200_driver = {
388 .driver = {
389 .owner = THIS_MODULE,
390 .name = "itg3200",
391 },
392 .id_table = itg3200_id,
393 .probe = itg3200_probe,
394 .remove = itg3200_remove,
395};
396
397module_i2c_driver(itg3200_driver);
398
399MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
400MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
401MODULE_LICENSE("GPL v2");