xref: /openbmc/linux/drivers/iio/gyro/itg3200_core.c (revision 0ef9d78b)
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/slab.h>
19 #include <linux/stat.h>
20 #include <linux/module.h>
21 #include <linux/mutex.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 	struct itg3200 *st = iio_priv(indio_dev);
136 	int ret;
137 	u8 t;
138 
139 	switch (mask) {
140 	case IIO_CHAN_INFO_SAMP_FREQ:
141 		if (val == 0 || val2 != 0)
142 			return -EINVAL;
143 
144 		mutex_lock(&st->lock);
145 
146 		ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
147 		if (ret) {
148 			mutex_unlock(&st->lock);
149 			return ret;
150 		}
151 		t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
152 
153 		ret = itg3200_write_reg_8(indio_dev,
154 					  ITG3200_REG_SAMPLE_RATE_DIV,
155 					  t);
156 
157 		mutex_unlock(&st->lock);
158 		return ret;
159 
160 	default:
161 		return -EINVAL;
162 	}
163 }
164 
165 /*
166  * Reset device and internal registers to the power-up-default settings
167  * Use the gyro clock as reference, as suggested by the datasheet
168  */
169 static int itg3200_reset(struct iio_dev *indio_dev)
170 {
171 	struct itg3200 *st = iio_priv(indio_dev);
172 	int ret;
173 
174 	dev_dbg(&st->i2c->dev, "reset device");
175 
176 	ret = itg3200_write_reg_8(indio_dev,
177 			ITG3200_REG_POWER_MANAGEMENT,
178 			ITG3200_RESET);
179 	if (ret) {
180 		dev_err(&st->i2c->dev, "error resetting device");
181 		goto error_ret;
182 	}
183 
184 	/* Wait for PLL (1ms according to datasheet) */
185 	udelay(1500);
186 
187 	ret = itg3200_write_reg_8(indio_dev,
188 			ITG3200_REG_IRQ_CONFIG,
189 			ITG3200_IRQ_ACTIVE_HIGH |
190 			ITG3200_IRQ_PUSH_PULL |
191 			ITG3200_IRQ_LATCH_50US_PULSE |
192 			ITG3200_IRQ_LATCH_CLEAR_ANY);
193 
194 	if (ret)
195 		dev_err(&st->i2c->dev, "error init device");
196 
197 error_ret:
198 	return ret;
199 }
200 
201 /* itg3200_enable_full_scale() - Disables the digital low pass filter */
202 static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
203 {
204 	u8 val;
205 	int ret;
206 
207 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
208 	if (ret)
209 		goto err_ret;
210 
211 	val |= ITG3200_DLPF_FS_SEL_2000;
212 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
213 
214 err_ret:
215 	return ret;
216 }
217 
218 static int itg3200_initial_setup(struct iio_dev *indio_dev)
219 {
220 	struct itg3200 *st = iio_priv(indio_dev);
221 	int ret;
222 	u8 val;
223 
224 	ret = itg3200_reset(indio_dev);
225 	if (ret)
226 		goto err_ret;
227 
228 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
229 	if (ret)
230 		goto err_ret;
231 
232 	if (((val >> 1) & 0x3f) != 0x34) {
233 		dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
234 		ret = -ENXIO;
235 		goto err_ret;
236 	}
237 
238 	ret = itg3200_enable_full_scale(indio_dev);
239 err_ret:
240 	return ret;
241 }
242 
243 static const struct iio_mount_matrix *
244 itg3200_get_mount_matrix(const struct iio_dev *indio_dev,
245 			  const struct iio_chan_spec *chan)
246 {
247 	struct itg3200 *data = iio_priv(indio_dev);
248 
249 	return &data->orientation;
250 }
251 
252 static const struct iio_chan_spec_ext_info itg3200_ext_info[] = {
253 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, itg3200_get_mount_matrix),
254 	{ }
255 };
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_separate = BIT(IIO_CHAN_INFO_RAW), \
265 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
266 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
267 	.address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
268 	.scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
269 	.scan_type = ITG3200_ST, \
270 	.ext_info = itg3200_ext_info, \
271 }
272 
273 static const struct iio_chan_spec itg3200_channels[] = {
274 	{
275 		.type = IIO_TEMP,
276 		.channel2 = IIO_NO_MOD,
277 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
278 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
279 		BIT(IIO_CHAN_INFO_SCALE),
280 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
281 		.address = ITG3200_REG_TEMP_OUT_H,
282 		.scan_index = ITG3200_SCAN_TEMP,
283 		.scan_type = ITG3200_ST,
284 	},
285 	ITG3200_GYRO_CHAN(X),
286 	ITG3200_GYRO_CHAN(Y),
287 	ITG3200_GYRO_CHAN(Z),
288 	IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
289 };
290 
291 static const struct iio_info itg3200_info = {
292 	.read_raw = &itg3200_read_raw,
293 	.write_raw = &itg3200_write_raw,
294 };
295 
296 static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
297 
298 static int itg3200_probe(struct i2c_client *client)
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, &st->orientation);
313 	if (ret)
314 		return ret;
315 
316 	i2c_set_clientdata(client, indio_dev);
317 	st->i2c = client;
318 
319 	indio_dev->name = client->dev.driver->name;
320 	indio_dev->channels = itg3200_channels;
321 	indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
322 	indio_dev->available_scan_masks = itg3200_available_scan_masks;
323 	indio_dev->info = &itg3200_info;
324 	indio_dev->modes = INDIO_DIRECT_MODE;
325 
326 	ret = itg3200_buffer_configure(indio_dev);
327 	if (ret)
328 		return ret;
329 
330 	if (client->irq) {
331 		ret = itg3200_probe_trigger(indio_dev);
332 		if (ret)
333 			goto error_unconfigure_buffer;
334 	}
335 
336 	ret = itg3200_initial_setup(indio_dev);
337 	if (ret)
338 		goto error_remove_trigger;
339 
340 	mutex_init(&st->lock);
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 void 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 
368 static int itg3200_suspend(struct device *dev)
369 {
370 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
371 	struct itg3200 *st = iio_priv(indio_dev);
372 
373 	dev_dbg(&st->i2c->dev, "suspend device");
374 
375 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
376 				   ITG3200_SLEEP);
377 }
378 
379 static int itg3200_resume(struct device *dev)
380 {
381 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
382 
383 	return itg3200_initial_setup(indio_dev);
384 }
385 
386 static DEFINE_SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend,
387 				itg3200_resume);
388 
389 static const struct i2c_device_id itg3200_id[] = {
390 	{ "itg3200", 0 },
391 	{ }
392 };
393 MODULE_DEVICE_TABLE(i2c, itg3200_id);
394 
395 static const struct of_device_id itg3200_of_match[] = {
396 	{ .compatible = "invensense,itg3200" },
397 	{ }
398 };
399 MODULE_DEVICE_TABLE(of, itg3200_of_match);
400 
401 static struct i2c_driver itg3200_driver = {
402 	.driver = {
403 		.name	= "itg3200",
404 		.of_match_table = itg3200_of_match,
405 		.pm	= pm_sleep_ptr(&itg3200_pm_ops),
406 	},
407 	.id_table	= itg3200_id,
408 	.probe		= itg3200_probe,
409 	.remove		= itg3200_remove,
410 };
411 
412 module_i2c_driver(itg3200_driver);
413 
414 MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
415 MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
416 MODULE_LICENSE("GPL v2");
417