xref: /openbmc/linux/drivers/iio/gyro/itg3200_core.c (revision 07c7c6bf)
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 
35 int 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 
43 int 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 
56 static 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 
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)
90 {
91 	int ret = 0;
92 	u8 reg;
93 	u8 regval;
94 
95 	switch (info) {
96 	case IIO_CHAN_INFO_RAW:
97 		reg = (u8)chan->address;
98 		ret = itg3200_read_reg_s16(indio_dev, reg, val);
99 		return IIO_VAL_INT;
100 	case IIO_CHAN_INFO_SCALE:
101 		*val = 0;
102 		if (chan->type == IIO_TEMP)
103 			*val2 = 1000000000/280;
104 		else
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 */
109 		*val = 23000;
110 		return IIO_VAL_INT;
111 	case IIO_CHAN_INFO_SAMP_FREQ:
112 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
113 		if (ret)
114 			return ret;
115 
116 		*val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
117 
118 		ret = itg3200_read_reg_8(indio_dev,
119 					 ITG3200_REG_SAMPLE_RATE_DIV,
120 					 &regval);
121 		if (ret)
122 			return ret;
123 
124 		*val /= regval + 1;
125 		return IIO_VAL_INT;
126 
127 	default:
128 		return -EINVAL;
129 	}
130 }
131 
132 static int itg3200_write_raw(struct iio_dev *indio_dev,
133 			     struct iio_chan_spec const *chan,
134 			     int val,
135 			     int val2,
136 			     long mask)
137 {
138 	int ret;
139 	u8 t;
140 
141 	switch (mask) {
142 	case IIO_CHAN_INFO_SAMP_FREQ:
143 		if (val == 0 || val2 != 0)
144 			return -EINVAL;
145 
146 		mutex_lock(&indio_dev->mlock);
147 
148 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
149 		if (ret) {
150 			mutex_unlock(&indio_dev->mlock);
151 			return ret;
152 		}
153 		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
154 
155 		ret = itg3200_write_reg_8(indio_dev,
156 					  ITG3200_REG_SAMPLE_RATE_DIV,
157 					  t);
158 
159 		mutex_unlock(&indio_dev->mlock);
160 	return ret;
161 
162 	default:
163 		return -EINVAL;
164 	}
165 }
166 
167 /*
168  * Reset device and internal registers to the power-up-default settings
169  * Use the gyro clock as reference, as suggested by the datasheet
170  */
171 static int itg3200_reset(struct iio_dev *indio_dev)
172 {
173 	struct itg3200 *st = iio_priv(indio_dev);
174 	int ret;
175 
176 	dev_dbg(&st->i2c->dev, "reset device");
177 
178 	ret = itg3200_write_reg_8(indio_dev,
179 			ITG3200_REG_POWER_MANAGEMENT,
180 			ITG3200_RESET);
181 	if (ret) {
182 		dev_err(&st->i2c->dev, "error resetting device");
183 		goto error_ret;
184 	}
185 
186 	/* Wait for PLL (1ms according to datasheet) */
187 	udelay(1500);
188 
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);
195 
196 	if (ret)
197 		dev_err(&st->i2c->dev, "error init device");
198 
199 error_ret:
200 	return ret;
201 }
202 
203 /* itg3200_enable_full_scale() - Disables the digital low pass filter */
204 static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
205 {
206 	u8 val;
207 	int ret;
208 
209 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
210 	if (ret)
211 		goto err_ret;
212 
213 	val |= ITG3200_DLPF_FS_SEL_2000;
214 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
215 
216 err_ret:
217 	return ret;
218 }
219 
220 static int itg3200_initial_setup(struct iio_dev *indio_dev)
221 {
222 	struct itg3200 *st = iio_priv(indio_dev);
223 	int ret;
224 	u8 val;
225 
226 	ret = itg3200_reset(indio_dev);
227 	if (ret)
228 		goto err_ret;
229 
230 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
231 	if (ret)
232 		goto err_ret;
233 
234 	if (((val >> 1) & 0x3f) != 0x34) {
235 		dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
236 		ret = -ENXIO;
237 		goto err_ret;
238 	}
239 
240 	ret = itg3200_enable_full_scale(indio_dev);
241 err_ret:
242 	return ret;
243 }
244 
245 static const struct iio_mount_matrix *
246 itg3200_get_mount_matrix(const struct iio_dev *indio_dev,
247 			  const struct iio_chan_spec *chan)
248 {
249 	struct itg3200 *data = iio_priv(indio_dev);
250 
251 	return &data->orientation;
252 }
253 
254 static const struct iio_chan_spec_ext_info itg3200_ext_info[] = {
255 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, itg3200_get_mount_matrix),
256 	{ }
257 };
258 
259 #define ITG3200_ST						\
260 	{ .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
261 
262 #define ITG3200_GYRO_CHAN(_mod) { \
263 	.type = IIO_ANGL_VEL, \
264 	.modified = 1, \
265 	.channel2 = IIO_MOD_ ## _mod, \
266 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
267 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
268 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
269 	.address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
270 	.scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
271 	.scan_type = ITG3200_ST, \
272 	.ext_info = itg3200_ext_info, \
273 }
274 
275 static const struct iio_chan_spec itg3200_channels[] = {
276 	{
277 		.type = IIO_TEMP,
278 		.channel2 = IIO_NO_MOD,
279 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
280 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
281 		BIT(IIO_CHAN_INFO_SCALE),
282 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
283 		.address = ITG3200_REG_TEMP_OUT_H,
284 		.scan_index = ITG3200_SCAN_TEMP,
285 		.scan_type = ITG3200_ST,
286 	},
287 	ITG3200_GYRO_CHAN(X),
288 	ITG3200_GYRO_CHAN(Y),
289 	ITG3200_GYRO_CHAN(Z),
290 	IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
291 };
292 
293 static const struct iio_info itg3200_info = {
294 	.read_raw = &itg3200_read_raw,
295 	.write_raw = &itg3200_write_raw,
296 };
297 
298 static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
299 
300 static int itg3200_probe(struct i2c_client *client,
301 		const struct i2c_device_id *id)
302 {
303 	int ret;
304 	struct itg3200 *st;
305 	struct iio_dev *indio_dev;
306 
307 	dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
308 
309 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
310 	if (!indio_dev)
311 		return -ENOMEM;
312 
313 	st = iio_priv(indio_dev);
314 
315 	ret = iio_read_mount_matrix(&client->dev, "mount-matrix",
316 				&st->orientation);
317 	if (ret)
318 		return ret;
319 
320 	i2c_set_clientdata(client, indio_dev);
321 	st->i2c = client;
322 
323 	indio_dev->dev.parent = &client->dev;
324 	indio_dev->name = client->dev.driver->name;
325 	indio_dev->channels = itg3200_channels;
326 	indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
327 	indio_dev->available_scan_masks = itg3200_available_scan_masks;
328 	indio_dev->info = &itg3200_info;
329 	indio_dev->modes = INDIO_DIRECT_MODE;
330 
331 	ret = itg3200_buffer_configure(indio_dev);
332 	if (ret)
333 		return ret;
334 
335 	if (client->irq) {
336 		ret = itg3200_probe_trigger(indio_dev);
337 		if (ret)
338 			goto error_unconfigure_buffer;
339 	}
340 
341 	ret = itg3200_initial_setup(indio_dev);
342 	if (ret)
343 		goto error_remove_trigger;
344 
345 	ret = iio_device_register(indio_dev);
346 	if (ret)
347 		goto error_remove_trigger;
348 
349 	return 0;
350 
351 error_remove_trigger:
352 	if (client->irq)
353 		itg3200_remove_trigger(indio_dev);
354 error_unconfigure_buffer:
355 	itg3200_buffer_unconfigure(indio_dev);
356 	return ret;
357 }
358 
359 static int itg3200_remove(struct i2c_client *client)
360 {
361 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
362 
363 	iio_device_unregister(indio_dev);
364 
365 	if (client->irq)
366 		itg3200_remove_trigger(indio_dev);
367 
368 	itg3200_buffer_unconfigure(indio_dev);
369 
370 	return 0;
371 }
372 
373 static int __maybe_unused itg3200_suspend(struct device *dev)
374 {
375 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
376 	struct itg3200 *st = iio_priv(indio_dev);
377 
378 	dev_dbg(&st->i2c->dev, "suspend device");
379 
380 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
381 				   ITG3200_SLEEP);
382 }
383 
384 static int __maybe_unused itg3200_resume(struct device *dev)
385 {
386 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
387 
388 	return itg3200_initial_setup(indio_dev);
389 }
390 
391 static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
392 
393 static const struct i2c_device_id itg3200_id[] = {
394 	{ "itg3200", 0 },
395 	{ }
396 };
397 MODULE_DEVICE_TABLE(i2c, itg3200_id);
398 
399 static const struct of_device_id itg3200_of_match[] = {
400 	{ .compatible = "invensense,itg3200" },
401 	{ }
402 };
403 MODULE_DEVICE_TABLE(of, itg3200_of_match);
404 
405 static struct i2c_driver itg3200_driver = {
406 	.driver = {
407 		.name	= "itg3200",
408 		.of_match_table = itg3200_of_match,
409 		.pm	= &itg3200_pm_ops,
410 	},
411 	.id_table	= itg3200_id,
412 	.probe		= itg3200_probe,
413 	.remove		= itg3200_remove,
414 };
415 
416 module_i2c_driver(itg3200_driver);
417 
418 MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
419 MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
420 MODULE_LICENSE("GPL v2");
421