xref: /openbmc/linux/drivers/iio/gyro/itg3200_core.c (revision 03638e62)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * itg3200_core.c -- support InvenSense ITG3200
4  *                   Digital 3-Axis Gyroscope driver
5  *
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>
9  *
10  * TODO:
11  * - Support digital low pass filter
12  * - Support power management
13  */
14 
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/buffer.h>
28 
29 #include <linux/iio/gyro/itg3200.h>
30 
31 
32 int itg3200_write_reg_8(struct iio_dev *indio_dev,
33 		u8 reg_address, u8 val)
34 {
35 	struct itg3200 *st = iio_priv(indio_dev);
36 
37 	return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
38 }
39 
40 int itg3200_read_reg_8(struct iio_dev *indio_dev,
41 		u8 reg_address, u8 *val)
42 {
43 	struct itg3200 *st = iio_priv(indio_dev);
44 	int ret;
45 
46 	ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
47 	if (ret < 0)
48 		return ret;
49 	*val = ret;
50 	return 0;
51 }
52 
53 static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
54 		int *val)
55 {
56 	struct itg3200 *st = iio_priv(indio_dev);
57 	struct i2c_client *client = st->i2c;
58 	int ret;
59 	s16 out;
60 
61 	struct i2c_msg msg[2] = {
62 		{
63 			.addr = client->addr,
64 			.flags = client->flags,
65 			.len = 1,
66 			.buf = (char *)&lower_reg_address,
67 		},
68 		{
69 			.addr = client->addr,
70 			.flags = client->flags | I2C_M_RD,
71 			.len = 2,
72 			.buf = (char *)&out,
73 		},
74 	};
75 
76 	lower_reg_address |= 0x80;
77 	ret = i2c_transfer(client->adapter, msg, 2);
78 	be16_to_cpus(&out);
79 	*val = out;
80 
81 	return (ret == 2) ? 0 : ret;
82 }
83 
84 static int itg3200_read_raw(struct iio_dev *indio_dev,
85 		const struct iio_chan_spec *chan,
86 		int *val, int *val2, long info)
87 {
88 	int ret = 0;
89 	u8 reg;
90 	u8 regval;
91 
92 	switch (info) {
93 	case IIO_CHAN_INFO_RAW:
94 		reg = (u8)chan->address;
95 		ret = itg3200_read_reg_s16(indio_dev, reg, val);
96 		return IIO_VAL_INT;
97 	case IIO_CHAN_INFO_SCALE:
98 		*val = 0;
99 		if (chan->type == IIO_TEMP)
100 			*val2 = 1000000000/280;
101 		else
102 			*val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
103 		return IIO_VAL_INT_PLUS_NANO;
104 	case IIO_CHAN_INFO_OFFSET:
105 		/* Only the temperature channel has an offset */
106 		*val = 23000;
107 		return IIO_VAL_INT;
108 	case IIO_CHAN_INFO_SAMP_FREQ:
109 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
110 		if (ret)
111 			return ret;
112 
113 		*val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
114 
115 		ret = itg3200_read_reg_8(indio_dev,
116 					 ITG3200_REG_SAMPLE_RATE_DIV,
117 					 &regval);
118 		if (ret)
119 			return ret;
120 
121 		*val /= regval + 1;
122 		return IIO_VAL_INT;
123 
124 	default:
125 		return -EINVAL;
126 	}
127 }
128 
129 static int itg3200_write_raw(struct iio_dev *indio_dev,
130 			     struct iio_chan_spec const *chan,
131 			     int val,
132 			     int val2,
133 			     long mask)
134 {
135 	int ret;
136 	u8 t;
137 
138 	switch (mask) {
139 	case IIO_CHAN_INFO_SAMP_FREQ:
140 		if (val == 0 || val2 != 0)
141 			return -EINVAL;
142 
143 		mutex_lock(&indio_dev->mlock);
144 
145 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
146 		if (ret) {
147 			mutex_unlock(&indio_dev->mlock);
148 			return ret;
149 		}
150 		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
151 
152 		ret = itg3200_write_reg_8(indio_dev,
153 					  ITG3200_REG_SAMPLE_RATE_DIV,
154 					  t);
155 
156 		mutex_unlock(&indio_dev->mlock);
157 	return ret;
158 
159 	default:
160 		return -EINVAL;
161 	}
162 }
163 
164 /*
165  * Reset device and internal registers to the power-up-default settings
166  * Use the gyro clock as reference, as suggested by the datasheet
167  */
168 static int itg3200_reset(struct iio_dev *indio_dev)
169 {
170 	struct itg3200 *st = iio_priv(indio_dev);
171 	int ret;
172 
173 	dev_dbg(&st->i2c->dev, "reset device");
174 
175 	ret = itg3200_write_reg_8(indio_dev,
176 			ITG3200_REG_POWER_MANAGEMENT,
177 			ITG3200_RESET);
178 	if (ret) {
179 		dev_err(&st->i2c->dev, "error resetting device");
180 		goto error_ret;
181 	}
182 
183 	/* Wait for PLL (1ms according to datasheet) */
184 	udelay(1500);
185 
186 	ret = itg3200_write_reg_8(indio_dev,
187 			ITG3200_REG_IRQ_CONFIG,
188 			ITG3200_IRQ_ACTIVE_HIGH |
189 			ITG3200_IRQ_PUSH_PULL |
190 			ITG3200_IRQ_LATCH_50US_PULSE |
191 			ITG3200_IRQ_LATCH_CLEAR_ANY);
192 
193 	if (ret)
194 		dev_err(&st->i2c->dev, "error init device");
195 
196 error_ret:
197 	return ret;
198 }
199 
200 /* itg3200_enable_full_scale() - Disables the digital low pass filter */
201 static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
202 {
203 	u8 val;
204 	int ret;
205 
206 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
207 	if (ret)
208 		goto err_ret;
209 
210 	val |= ITG3200_DLPF_FS_SEL_2000;
211 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
212 
213 err_ret:
214 	return ret;
215 }
216 
217 static int itg3200_initial_setup(struct iio_dev *indio_dev)
218 {
219 	struct itg3200 *st = iio_priv(indio_dev);
220 	int ret;
221 	u8 val;
222 
223 	ret = itg3200_reset(indio_dev);
224 	if (ret)
225 		goto err_ret;
226 
227 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
228 	if (ret)
229 		goto err_ret;
230 
231 	if (((val >> 1) & 0x3f) != 0x34) {
232 		dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
233 		ret = -ENXIO;
234 		goto err_ret;
235 	}
236 
237 	ret = itg3200_enable_full_scale(indio_dev);
238 err_ret:
239 	return ret;
240 }
241 
242 static const struct iio_mount_matrix *
243 itg3200_get_mount_matrix(const struct iio_dev *indio_dev,
244 			  const struct iio_chan_spec *chan)
245 {
246 	struct itg3200 *data = iio_priv(indio_dev);
247 
248 	return &data->orientation;
249 }
250 
251 static const struct iio_chan_spec_ext_info itg3200_ext_info[] = {
252 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, itg3200_get_mount_matrix),
253 	{ }
254 };
255 
256 #define ITG3200_ST						\
257 	{ .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
258 
259 #define ITG3200_GYRO_CHAN(_mod) { \
260 	.type = IIO_ANGL_VEL, \
261 	.modified = 1, \
262 	.channel2 = IIO_MOD_ ## _mod, \
263 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
264 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
265 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
266 	.address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
267 	.scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
268 	.scan_type = ITG3200_ST, \
269 	.ext_info = itg3200_ext_info, \
270 }
271 
272 static const struct iio_chan_spec itg3200_channels[] = {
273 	{
274 		.type = IIO_TEMP,
275 		.channel2 = IIO_NO_MOD,
276 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
277 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
278 		BIT(IIO_CHAN_INFO_SCALE),
279 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
280 		.address = ITG3200_REG_TEMP_OUT_H,
281 		.scan_index = ITG3200_SCAN_TEMP,
282 		.scan_type = ITG3200_ST,
283 	},
284 	ITG3200_GYRO_CHAN(X),
285 	ITG3200_GYRO_CHAN(Y),
286 	ITG3200_GYRO_CHAN(Z),
287 	IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
288 };
289 
290 static const struct iio_info itg3200_info = {
291 	.read_raw = &itg3200_read_raw,
292 	.write_raw = &itg3200_write_raw,
293 };
294 
295 static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
296 
297 static int itg3200_probe(struct i2c_client *client,
298 		const struct i2c_device_id *id)
299 {
300 	int ret;
301 	struct itg3200 *st;
302 	struct iio_dev *indio_dev;
303 
304 	dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
305 
306 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
307 	if (!indio_dev)
308 		return -ENOMEM;
309 
310 	st = iio_priv(indio_dev);
311 
312 	ret = iio_read_mount_matrix(&client->dev, "mount-matrix",
313 				&st->orientation);
314 	if (ret)
315 		return ret;
316 
317 	i2c_set_clientdata(client, indio_dev);
318 	st->i2c = client;
319 
320 	indio_dev->dev.parent = &client->dev;
321 	indio_dev->name = client->dev.driver->name;
322 	indio_dev->channels = itg3200_channels;
323 	indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
324 	indio_dev->available_scan_masks = itg3200_available_scan_masks;
325 	indio_dev->info = &itg3200_info;
326 	indio_dev->modes = INDIO_DIRECT_MODE;
327 
328 	ret = itg3200_buffer_configure(indio_dev);
329 	if (ret)
330 		return ret;
331 
332 	if (client->irq) {
333 		ret = itg3200_probe_trigger(indio_dev);
334 		if (ret)
335 			goto error_unconfigure_buffer;
336 	}
337 
338 	ret = itg3200_initial_setup(indio_dev);
339 	if (ret)
340 		goto error_remove_trigger;
341 
342 	ret = iio_device_register(indio_dev);
343 	if (ret)
344 		goto error_remove_trigger;
345 
346 	return 0;
347 
348 error_remove_trigger:
349 	if (client->irq)
350 		itg3200_remove_trigger(indio_dev);
351 error_unconfigure_buffer:
352 	itg3200_buffer_unconfigure(indio_dev);
353 	return ret;
354 }
355 
356 static int itg3200_remove(struct i2c_client *client)
357 {
358 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
359 
360 	iio_device_unregister(indio_dev);
361 
362 	if (client->irq)
363 		itg3200_remove_trigger(indio_dev);
364 
365 	itg3200_buffer_unconfigure(indio_dev);
366 
367 	return 0;
368 }
369 
370 static int __maybe_unused itg3200_suspend(struct device *dev)
371 {
372 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
373 	struct itg3200 *st = iio_priv(indio_dev);
374 
375 	dev_dbg(&st->i2c->dev, "suspend device");
376 
377 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
378 				   ITG3200_SLEEP);
379 }
380 
381 static int __maybe_unused itg3200_resume(struct device *dev)
382 {
383 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
384 
385 	return itg3200_initial_setup(indio_dev);
386 }
387 
388 static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
389 
390 static const struct i2c_device_id itg3200_id[] = {
391 	{ "itg3200", 0 },
392 	{ }
393 };
394 MODULE_DEVICE_TABLE(i2c, itg3200_id);
395 
396 static const struct of_device_id itg3200_of_match[] = {
397 	{ .compatible = "invensense,itg3200" },
398 	{ }
399 };
400 MODULE_DEVICE_TABLE(of, itg3200_of_match);
401 
402 static struct i2c_driver itg3200_driver = {
403 	.driver = {
404 		.name	= "itg3200",
405 		.of_match_table = itg3200_of_match,
406 		.pm	= &itg3200_pm_ops,
407 	},
408 	.id_table	= itg3200_id,
409 	.probe		= itg3200_probe,
410 	.remove		= itg3200_remove,
411 };
412 
413 module_i2c_driver(itg3200_driver);
414 
415 MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
416 MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
417 MODULE_LICENSE("GPL v2");
418