xref: /openbmc/linux/drivers/iio/gyro/itg3200_core.c (revision 84d517f3)
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 
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 
115 static ssize_t itg3200_read_frequency(struct device *dev,
116 		struct device_attribute *attr, char *buf)
117 {
118 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
119 	int ret, sps;
120 	u8 val;
121 
122 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
123 	if (ret)
124 		return ret;
125 
126 	sps = (val & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
127 
128 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, &val);
129 	if (ret)
130 		return ret;
131 
132 	sps /= val + 1;
133 
134 	return sprintf(buf, "%d\n", sps);
135 }
136 
137 static ssize_t itg3200_write_frequency(struct device *dev,
138 		struct device_attribute *attr,
139 		const char *buf,
140 		size_t len)
141 {
142 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
143 	unsigned val;
144 	int ret;
145 	u8 t;
146 
147 	ret = kstrtouint(buf, 10, &val);
148 	if (ret)
149 		return ret;
150 
151 	mutex_lock(&indio_dev->mlock);
152 
153 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
154 	if (ret)
155 		goto err_ret;
156 
157 	if (val == 0) {
158 		ret = -EINVAL;
159 		goto err_ret;
160 	}
161 	t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
162 
163 	ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, t);
164 
165 err_ret:
166 	mutex_unlock(&indio_dev->mlock);
167 
168 	return ret ? ret : len;
169 }
170 
171 /*
172  * Reset device and internal registers to the power-up-default settings
173  * Use the gyro clock as reference, as suggested by the datasheet
174  */
175 static int itg3200_reset(struct iio_dev *indio_dev)
176 {
177 	struct itg3200 *st = iio_priv(indio_dev);
178 	int ret;
179 
180 	dev_dbg(&st->i2c->dev, "reset device");
181 
182 	ret = itg3200_write_reg_8(indio_dev,
183 			ITG3200_REG_POWER_MANAGEMENT,
184 			ITG3200_RESET);
185 	if (ret) {
186 		dev_err(&st->i2c->dev, "error resetting device");
187 		goto error_ret;
188 	}
189 
190 	/* Wait for PLL (1ms according to datasheet) */
191 	udelay(1500);
192 
193 	ret = itg3200_write_reg_8(indio_dev,
194 			ITG3200_REG_IRQ_CONFIG,
195 			ITG3200_IRQ_ACTIVE_HIGH |
196 			ITG3200_IRQ_PUSH_PULL |
197 			ITG3200_IRQ_LATCH_50US_PULSE |
198 			ITG3200_IRQ_LATCH_CLEAR_ANY);
199 
200 	if (ret)
201 		dev_err(&st->i2c->dev, "error init device");
202 
203 error_ret:
204 	return ret;
205 }
206 
207 /* itg3200_enable_full_scale() - Disables the digital low pass filter */
208 static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
209 {
210 	u8 val;
211 	int ret;
212 
213 	ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
214 	if (ret)
215 		goto err_ret;
216 
217 	val |= ITG3200_DLPF_FS_SEL_2000;
218 	return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
219 
220 err_ret:
221 	return ret;
222 }
223 
224 static int itg3200_initial_setup(struct iio_dev *indio_dev)
225 {
226 	struct itg3200 *st = iio_priv(indio_dev);
227 	int ret;
228 	u8 val;
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_reset(indio_dev);
241 	if (ret)
242 		goto err_ret;
243 
244 	ret = itg3200_enable_full_scale(indio_dev);
245 err_ret:
246 	return ret;
247 }
248 
249 #define ITG3200_ST						\
250 	{ .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
251 
252 #define ITG3200_GYRO_CHAN(_mod) { \
253 	.type = IIO_ANGL_VEL, \
254 	.modified = 1, \
255 	.channel2 = IIO_MOD_ ## _mod, \
256 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
257 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
258 	.address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
259 	.scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
260 	.scan_type = ITG3200_ST, \
261 }
262 
263 static const struct iio_chan_spec itg3200_channels[] = {
264 	{
265 		.type = IIO_TEMP,
266 		.channel2 = IIO_NO_MOD,
267 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
268 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
269 		BIT(IIO_CHAN_INFO_SCALE),
270 		.address = ITG3200_REG_TEMP_OUT_H,
271 		.scan_index = ITG3200_SCAN_TEMP,
272 		.scan_type = ITG3200_ST,
273 	},
274 	ITG3200_GYRO_CHAN(X),
275 	ITG3200_GYRO_CHAN(Y),
276 	ITG3200_GYRO_CHAN(Z),
277 	IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
278 };
279 
280 /* IIO device attributes */
281 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, itg3200_read_frequency,
282 		itg3200_write_frequency);
283 
284 static struct attribute *itg3200_attributes[] = {
285 	&iio_dev_attr_sampling_frequency.dev_attr.attr,
286 	NULL
287 };
288 
289 static const struct attribute_group itg3200_attribute_group = {
290 	.attrs = itg3200_attributes,
291 };
292 
293 static const struct iio_info itg3200_info = {
294 	.attrs = &itg3200_attribute_group,
295 	.read_raw = &itg3200_read_raw,
296 	.driver_module = THIS_MODULE,
297 };
298 
299 static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
300 
301 static int itg3200_probe(struct i2c_client *client,
302 		const struct i2c_device_id *id)
303 {
304 	int ret;
305 	struct itg3200 *st;
306 	struct iio_dev *indio_dev;
307 
308 	dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
309 
310 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
311 	if (!indio_dev)
312 		return -ENOMEM;
313 
314 	st = iio_priv(indio_dev);
315 
316 	i2c_set_clientdata(client, indio_dev);
317 	st->i2c = client;
318 
319 	indio_dev->dev.parent = &client->dev;
320 	indio_dev->name = client->dev.driver->name;
321 	indio_dev->channels = itg3200_channels;
322 	indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
323 	indio_dev->available_scan_masks = itg3200_available_scan_masks;
324 	indio_dev->info = &itg3200_info;
325 	indio_dev->modes = INDIO_DIRECT_MODE;
326 
327 	ret = itg3200_buffer_configure(indio_dev);
328 	if (ret)
329 		return ret;
330 
331 	if (client->irq) {
332 		ret = itg3200_probe_trigger(indio_dev);
333 		if (ret)
334 			goto error_unconfigure_buffer;
335 	}
336 
337 	ret = itg3200_initial_setup(indio_dev);
338 	if (ret)
339 		goto error_remove_trigger;
340 
341 	ret = iio_device_register(indio_dev);
342 	if (ret)
343 		goto error_remove_trigger;
344 
345 	return 0;
346 
347 error_remove_trigger:
348 	if (client->irq)
349 		itg3200_remove_trigger(indio_dev);
350 error_unconfigure_buffer:
351 	itg3200_buffer_unconfigure(indio_dev);
352 	return ret;
353 }
354 
355 static int itg3200_remove(struct i2c_client *client)
356 {
357 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
358 
359 	iio_device_unregister(indio_dev);
360 
361 	if (client->irq)
362 		itg3200_remove_trigger(indio_dev);
363 
364 	itg3200_buffer_unconfigure(indio_dev);
365 
366 	return 0;
367 }
368 
369 static const struct i2c_device_id itg3200_id[] = {
370 	{ "itg3200", 0 },
371 	{ }
372 };
373 MODULE_DEVICE_TABLE(i2c, itg3200_id);
374 
375 static struct i2c_driver itg3200_driver = {
376 	.driver = {
377 		.owner  = THIS_MODULE,
378 		.name	= "itg3200",
379 	},
380 	.id_table	= itg3200_id,
381 	.probe		= itg3200_probe,
382 	.remove		= itg3200_remove,
383 };
384 
385 module_i2c_driver(itg3200_driver);
386 
387 MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
388 MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
389 MODULE_LICENSE("GPL v2");
390