2 * itg3200_core.c -- support InvenSense ITG3200
3 * Digital 3-Axis Gyroscope driver
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>
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.
14 * - Support digital low pass filter
15 * - Support power management
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>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/buffer.h>
32 #include <linux/iio/gyro/itg3200.h>
35 int itg3200_write_reg_8(struct iio_dev
*indio_dev
,
36 u8 reg_address
, u8 val
)
38 struct itg3200
*st
= iio_priv(indio_dev
);
40 return i2c_smbus_write_byte_data(st
->i2c
, 0x80 | reg_address
, val
);
43 int itg3200_read_reg_8(struct iio_dev
*indio_dev
,
44 u8 reg_address
, u8
*val
)
46 struct itg3200
*st
= iio_priv(indio_dev
);
49 ret
= i2c_smbus_read_byte_data(st
->i2c
, reg_address
);
56 static int itg3200_read_reg_s16(struct iio_dev
*indio_dev
, u8 lower_reg_address
,
59 struct itg3200
*st
= iio_priv(indio_dev
);
60 struct i2c_client
*client
= st
->i2c
;
64 struct i2c_msg msg
[2] = {
67 .flags
= client
->flags
,
69 .buf
= (char *)&lower_reg_address
,
73 .flags
= client
->flags
| I2C_M_RD
,
79 lower_reg_address
|= 0x80;
80 ret
= i2c_transfer(client
->adapter
, msg
, 2);
84 return (ret
== 2) ? 0 : ret
;
87 static int itg3200_read_raw(struct iio_dev
*indio_dev
,
88 const struct iio_chan_spec
*chan
,
89 int *val
, int *val2
, long info
)
96 case IIO_CHAN_INFO_RAW
:
97 reg
= (u8
)chan
->address
;
98 ret
= itg3200_read_reg_s16(indio_dev
, reg
, val
);
100 case IIO_CHAN_INFO_SCALE
:
102 if (chan
->type
== IIO_TEMP
)
103 *val2
= 1000000000/280;
105 *val2
= 1214142; /* (1 / 14,375) * (PI / 180) */
106 return IIO_VAL_INT_PLUS_NANO
;
107 case IIO_CHAN_INFO_OFFSET
:
108 /* Only the temperature channel has an offset */
111 case IIO_CHAN_INFO_SAMP_FREQ
:
112 ret
= itg3200_read_reg_8(indio_dev
, ITG3200_REG_DLPF
, ®val
);
116 *val
= (regval
& ITG3200_DLPF_CFG_MASK
) ? 1000 : 8000;
118 ret
= itg3200_read_reg_8(indio_dev
,
119 ITG3200_REG_SAMPLE_RATE_DIV
,
132 static int itg3200_write_raw(struct iio_dev
*indio_dev
,
133 struct iio_chan_spec
const *chan
,
142 case IIO_CHAN_INFO_SAMP_FREQ
:
143 if (val
== 0 || val2
!= 0)
146 mutex_lock(&indio_dev
->mlock
);
148 ret
= itg3200_read_reg_8(indio_dev
, ITG3200_REG_DLPF
, &t
);
150 mutex_unlock(&indio_dev
->mlock
);
153 t
= ((t
& ITG3200_DLPF_CFG_MASK
) ? 1000u : 8000u) / val
- 1;
155 ret
= itg3200_write_reg_8(indio_dev
,
156 ITG3200_REG_SAMPLE_RATE_DIV
,
159 mutex_unlock(&indio_dev
->mlock
);
168 * Reset device and internal registers to the power-up-default settings
169 * Use the gyro clock as reference, as suggested by the datasheet
171 static int itg3200_reset(struct iio_dev
*indio_dev
)
173 struct itg3200
*st
= iio_priv(indio_dev
);
176 dev_dbg(&st
->i2c
->dev
, "reset device");
178 ret
= itg3200_write_reg_8(indio_dev
,
179 ITG3200_REG_POWER_MANAGEMENT
,
182 dev_err(&st
->i2c
->dev
, "error resetting device");
186 /* Wait for PLL (1ms according to datasheet) */
189 ret
= itg3200_write_reg_8(indio_dev
,
190 ITG3200_REG_IRQ_CONFIG
,
191 ITG3200_IRQ_ACTIVE_HIGH
|
192 ITG3200_IRQ_PUSH_PULL
|
193 ITG3200_IRQ_LATCH_50US_PULSE
|
194 ITG3200_IRQ_LATCH_CLEAR_ANY
);
197 dev_err(&st
->i2c
->dev
, "error init device");
203 /* itg3200_enable_full_scale() - Disables the digital low pass filter */
204 static int itg3200_enable_full_scale(struct iio_dev
*indio_dev
)
209 ret
= itg3200_read_reg_8(indio_dev
, ITG3200_REG_DLPF
, &val
);
213 val
|= ITG3200_DLPF_FS_SEL_2000
;
214 return itg3200_write_reg_8(indio_dev
, ITG3200_REG_DLPF
, val
);
220 static int itg3200_initial_setup(struct iio_dev
*indio_dev
)
222 struct itg3200
*st
= iio_priv(indio_dev
);
226 ret
= itg3200_read_reg_8(indio_dev
, ITG3200_REG_ADDRESS
, &val
);
230 if (((val
>> 1) & 0x3f) != 0x34) {
231 dev_err(&st
->i2c
->dev
, "invalid reg value 0x%02x", val
);
236 ret
= itg3200_reset(indio_dev
);
240 ret
= itg3200_enable_full_scale(indio_dev
);
246 { .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
248 #define ITG3200_GYRO_CHAN(_mod) { \
249 .type = IIO_ANGL_VEL, \
251 .channel2 = IIO_MOD_ ## _mod, \
252 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
253 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
254 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
255 .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
256 .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
257 .scan_type = ITG3200_ST, \
260 static const struct iio_chan_spec itg3200_channels
[] = {
263 .channel2
= IIO_NO_MOD
,
264 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
265 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
266 BIT(IIO_CHAN_INFO_SCALE
),
267 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
268 .address
= ITG3200_REG_TEMP_OUT_H
,
269 .scan_index
= ITG3200_SCAN_TEMP
,
270 .scan_type
= ITG3200_ST
,
272 ITG3200_GYRO_CHAN(X
),
273 ITG3200_GYRO_CHAN(Y
),
274 ITG3200_GYRO_CHAN(Z
),
275 IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS
),
278 static const struct iio_info itg3200_info
= {
279 .read_raw
= &itg3200_read_raw
,
280 .write_raw
= &itg3200_write_raw
,
281 .driver_module
= THIS_MODULE
,
284 static const unsigned long itg3200_available_scan_masks
[] = { 0xffffffff, 0x0 };
286 static int itg3200_probe(struct i2c_client
*client
,
287 const struct i2c_device_id
*id
)
291 struct iio_dev
*indio_dev
;
293 dev_dbg(&client
->dev
, "probe I2C dev with IRQ %i", client
->irq
);
295 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
299 st
= iio_priv(indio_dev
);
301 i2c_set_clientdata(client
, indio_dev
);
304 indio_dev
->dev
.parent
= &client
->dev
;
305 indio_dev
->name
= client
->dev
.driver
->name
;
306 indio_dev
->channels
= itg3200_channels
;
307 indio_dev
->num_channels
= ARRAY_SIZE(itg3200_channels
);
308 indio_dev
->available_scan_masks
= itg3200_available_scan_masks
;
309 indio_dev
->info
= &itg3200_info
;
310 indio_dev
->modes
= INDIO_DIRECT_MODE
;
312 ret
= itg3200_buffer_configure(indio_dev
);
317 ret
= itg3200_probe_trigger(indio_dev
);
319 goto error_unconfigure_buffer
;
322 ret
= itg3200_initial_setup(indio_dev
);
324 goto error_remove_trigger
;
326 ret
= iio_device_register(indio_dev
);
328 goto error_remove_trigger
;
332 error_remove_trigger
:
334 itg3200_remove_trigger(indio_dev
);
335 error_unconfigure_buffer
:
336 itg3200_buffer_unconfigure(indio_dev
);
340 static int itg3200_remove(struct i2c_client
*client
)
342 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
344 iio_device_unregister(indio_dev
);
347 itg3200_remove_trigger(indio_dev
);
349 itg3200_buffer_unconfigure(indio_dev
);
354 static const struct i2c_device_id itg3200_id
[] = {
358 MODULE_DEVICE_TABLE(i2c
, itg3200_id
);
360 static struct i2c_driver itg3200_driver
= {
362 .owner
= THIS_MODULE
,
365 .id_table
= itg3200_id
,
366 .probe
= itg3200_probe
,
367 .remove
= itg3200_remove
,
370 module_i2c_driver(itg3200_driver
);
372 MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
373 MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
374 MODULE_LICENSE("GPL v2");