PageRenderTime 85ms CodeModel.GetById 33ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/input/misc/mpu3050.c

http://github.com/mirrors/linux
C | 481 lines | 293 code | 66 blank | 122 comment | 14 complexity | cd113ad7c7646e6e54e4e5b4c5ca6b82 MD5 | raw file
  1/*
  2 * MPU3050 Tri-axis gyroscope driver
  3 *
  4 * Copyright (C) 2011 Wistron Co.Ltd
  5 * Joseph Lai <joseph_lai@wistron.com>
  6 *
  7 * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
  8 *
  9 * This is a 'lite' version of the driver, while we consider the right way
 10 * to present the other features to user space. In particular it requires the
 11 * device has an IRQ, and it only provides an input interface, so is not much
 12 * use for device orientation. A fuller version is available from the Meego
 13 * tree.
 14 *
 15 * This program is based on bma023.c.
 16 *
 17 * This program is free software; you can redistribute it and/or modify
 18 * it under the terms of the GNU General Public License as published by
 19 * the Free Software Foundation; version 2 of the License.
 20 *
 21 * This program is distributed in the hope that it will be useful, but
 22 * WITHOUT ANY WARRANTY; without even the implied warranty of
 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 24 * General Public License for more details.
 25 *
 26 * You should have received a copy of the GNU General Public License along
 27 * with this program; if not, write to the Free Software Foundation, Inc.,
 28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 29 *
 30 */
 31
 32#include <linux/module.h>
 33#include <linux/interrupt.h>
 34#include <linux/platform_device.h>
 35#include <linux/mutex.h>
 36#include <linux/err.h>
 37#include <linux/i2c.h>
 38#include <linux/input.h>
 39#include <linux/delay.h>
 40#include <linux/slab.h>
 41#include <linux/pm_runtime.h>
 42
 43#define MPU3050_CHIP_ID		0x69
 44
 45#define MPU3050_AUTO_DELAY	1000
 46
 47#define MPU3050_MIN_VALUE	-32768
 48#define MPU3050_MAX_VALUE	32767
 49
 50#define MPU3050_DEFAULT_POLL_INTERVAL	200
 51#define MPU3050_DEFAULT_FS_RANGE	3
 52
 53/* Register map */
 54#define MPU3050_CHIP_ID_REG	0x00
 55#define MPU3050_SMPLRT_DIV	0x15
 56#define MPU3050_DLPF_FS_SYNC	0x16
 57#define MPU3050_INT_CFG		0x17
 58#define MPU3050_XOUT_H		0x1D
 59#define MPU3050_PWR_MGM		0x3E
 60#define MPU3050_PWR_MGM_POS	6
 61
 62/* Register bits */
 63
 64/* DLPF_FS_SYNC */
 65#define MPU3050_EXT_SYNC_NONE		0x00
 66#define MPU3050_EXT_SYNC_TEMP		0x20
 67#define MPU3050_EXT_SYNC_GYROX		0x40
 68#define MPU3050_EXT_SYNC_GYROY		0x60
 69#define MPU3050_EXT_SYNC_GYROZ		0x80
 70#define MPU3050_EXT_SYNC_ACCELX	0xA0
 71#define MPU3050_EXT_SYNC_ACCELY	0xC0
 72#define MPU3050_EXT_SYNC_ACCELZ	0xE0
 73#define MPU3050_EXT_SYNC_MASK		0xE0
 74#define MPU3050_FS_250DPS		0x00
 75#define MPU3050_FS_500DPS		0x08
 76#define MPU3050_FS_1000DPS		0x10
 77#define MPU3050_FS_2000DPS		0x18
 78#define MPU3050_FS_MASK		0x18
 79#define MPU3050_DLPF_CFG_256HZ_NOLPF2	0x00
 80#define MPU3050_DLPF_CFG_188HZ		0x01
 81#define MPU3050_DLPF_CFG_98HZ		0x02
 82#define MPU3050_DLPF_CFG_42HZ		0x03
 83#define MPU3050_DLPF_CFG_20HZ		0x04
 84#define MPU3050_DLPF_CFG_10HZ		0x05
 85#define MPU3050_DLPF_CFG_5HZ		0x06
 86#define MPU3050_DLPF_CFG_2100HZ_NOLPF	0x07
 87#define MPU3050_DLPF_CFG_MASK		0x07
 88/* INT_CFG */
 89#define MPU3050_RAW_RDY_EN		0x01
 90#define MPU3050_MPU_RDY_EN		0x02
 91#define MPU3050_LATCH_INT_EN		0x04
 92/* PWR_MGM */
 93#define MPU3050_PWR_MGM_PLL_X		0x01
 94#define MPU3050_PWR_MGM_PLL_Y		0x02
 95#define MPU3050_PWR_MGM_PLL_Z		0x03
 96#define MPU3050_PWR_MGM_CLKSEL		0x07
 97#define MPU3050_PWR_MGM_STBY_ZG	0x08
 98#define MPU3050_PWR_MGM_STBY_YG	0x10
 99#define MPU3050_PWR_MGM_STBY_XG	0x20
100#define MPU3050_PWR_MGM_SLEEP		0x40
101#define MPU3050_PWR_MGM_RESET		0x80
102#define MPU3050_PWR_MGM_MASK		0x40
103
104struct axis_data {
105	s16 x;
106	s16 y;
107	s16 z;
108};
109
110struct mpu3050_sensor {
111	struct i2c_client *client;
112	struct device *dev;
113	struct input_dev *idev;
114};
115
116/**
117 *	mpu3050_xyz_read_reg	-	read the axes values
118 *	@buffer: provide register addr and get register
119 *	@length: length of register
120 *
121 *	Reads the register values in one transaction or returns a negative
122 *	error code on failure.
123 */
124static int mpu3050_xyz_read_reg(struct i2c_client *client,
125			       u8 *buffer, int length)
126{
127	/*
128	 * Annoying we can't make this const because the i2c layer doesn't
129	 * declare input buffers const.
130	 */
131	char cmd = MPU3050_XOUT_H;
132	struct i2c_msg msg[] = {
133		{
134			.addr = client->addr,
135			.flags = 0,
136			.len = 1,
137			.buf = &cmd,
138		},
139		{
140			.addr = client->addr,
141			.flags = I2C_M_RD,
142			.len = length,
143			.buf = buffer,
144		},
145	};
146
147	return i2c_transfer(client->adapter, msg, 2);
148}
149
150/**
151 *	mpu3050_read_xyz	-	get co-ordinates from device
152 *	@client: i2c address of sensor
153 *	@coords: co-ordinates to update
154 *
155 *	Return the converted X Y and Z co-ordinates from the sensor device
156 */
157static void mpu3050_read_xyz(struct i2c_client *client,
158			     struct axis_data *coords)
159{
160	u16 buffer[3];
161
162	mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
163	coords->x = be16_to_cpu(buffer[0]);
164	coords->y = be16_to_cpu(buffer[1]);
165	coords->z = be16_to_cpu(buffer[2]);
166	dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
167					coords->x, coords->y, coords->z);
168}
169
170/**
171 *	mpu3050_set_power_mode	-	set the power mode
172 *	@client: i2c client for the sensor
173 *	@val: value to switch on/off of power, 1: normal power, 0: low power
174 *
175 *	Put device to normal-power mode or low-power mode.
176 */
177static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
178{
179	u8 value;
180
181	value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
182	value = (value & ~MPU3050_PWR_MGM_MASK) |
183		(((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
184		 MPU3050_PWR_MGM_MASK);
185	i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
186}
187
188/**
189 *	mpu3050_input_open	-	called on input event open
190 *	@input: input dev of opened device
191 *
192 *	The input layer calls this function when input event is opened. The
193 *	function will push the device to resume. Then, the device is ready
194 *	to provide data.
195 */
196static int mpu3050_input_open(struct input_dev *input)
197{
198	struct mpu3050_sensor *sensor = input_get_drvdata(input);
199	int error;
200
201	pm_runtime_get(sensor->dev);
202
203	/* Enable interrupts */
204	error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
205					  MPU3050_LATCH_INT_EN |
206					  MPU3050_RAW_RDY_EN |
207					  MPU3050_MPU_RDY_EN);
208	if (error < 0) {
209		pm_runtime_put(sensor->dev);
210		return error;
211	}
212
213	return 0;
214}
215
216/**
217 *	mpu3050_input_close	-	called on input event close
218 *	@input: input dev of closed device
219 *
220 *	The input layer calls this function when input event is closed. The
221 *	function will push the device to suspend.
222 */
223static void mpu3050_input_close(struct input_dev *input)
224{
225	struct mpu3050_sensor *sensor = input_get_drvdata(input);
226
227	pm_runtime_put(sensor->dev);
228}
229
230/**
231 *	mpu3050_interrupt_thread	-	handle an IRQ
232 *	@irq: interrupt numner
233 *	@data: the sensor
234 *
235 *	Called by the kernel single threaded after an interrupt occurs. Read
236 *	the sensor data and generate an input event for it.
237 */
238static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
239{
240	struct mpu3050_sensor *sensor = data;
241	struct axis_data axis;
242
243	mpu3050_read_xyz(sensor->client, &axis);
244
245	input_report_abs(sensor->idev, ABS_X, axis.x);
246	input_report_abs(sensor->idev, ABS_Y, axis.y);
247	input_report_abs(sensor->idev, ABS_Z, axis.z);
248	input_sync(sensor->idev);
249
250	return IRQ_HANDLED;
251}
252
253/**
254 *	mpu3050_hw_init	-	initialize hardware
255 *	@sensor: the sensor
256 *
257 *	Called during device probe; configures the sampling method.
258 */
259static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
260{
261	struct i2c_client *client = sensor->client;
262	int ret;
263	u8 reg;
264
265	/* Reset */
266	ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
267					MPU3050_PWR_MGM_RESET);
268	if (ret < 0)
269		return ret;
270
271	ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
272	if (ret < 0)
273		return ret;
274
275	ret &= ~MPU3050_PWR_MGM_CLKSEL;
276	ret |= MPU3050_PWR_MGM_PLL_Z;
277	ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
278	if (ret < 0)
279		return ret;
280
281	/* Output frequency divider. The poll interval */
282	ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
283					MPU3050_DEFAULT_POLL_INTERVAL - 1);
284	if (ret < 0)
285		return ret;
286
287	/* Set low pass filter and full scale */
288	reg = MPU3050_DEFAULT_FS_RANGE;
289	reg |= MPU3050_DLPF_CFG_42HZ << 3;
290	reg |= MPU3050_EXT_SYNC_NONE << 5;
291	ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
292	if (ret < 0)
293		return ret;
294
295	return 0;
296}
297
298/**
299 *	mpu3050_probe	-	device detection callback
300 *	@client: i2c client of found device
301 *	@id: id match information
302 *
303 *	The I2C layer calls us when it believes a sensor is present at this
304 *	address. Probe to see if this is correct and to validate the device.
305 *
306 *	If present install the relevant sysfs interfaces and input device.
307 */
308static int mpu3050_probe(struct i2c_client *client,
309				   const struct i2c_device_id *id)
310{
311	struct mpu3050_sensor *sensor;
312	struct input_dev *idev;
313	int ret;
314	int error;
315
316	sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
317	idev = input_allocate_device();
318	if (!sensor || !idev) {
319		dev_err(&client->dev, "failed to allocate driver data\n");
320		error = -ENOMEM;
321		goto err_free_mem;
322	}
323
324	sensor->client = client;
325	sensor->dev = &client->dev;
326	sensor->idev = idev;
327
328	mpu3050_set_power_mode(client, 1);
329	msleep(10);
330
331	ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
332	if (ret < 0) {
333		dev_err(&client->dev, "failed to detect device\n");
334		error = -ENXIO;
335		goto err_free_mem;
336	}
337
338	if (ret != MPU3050_CHIP_ID) {
339		dev_err(&client->dev, "unsupported chip id\n");
340		error = -ENXIO;
341		goto err_free_mem;
342	}
343
344	idev->name = "MPU3050";
345	idev->id.bustype = BUS_I2C;
346	idev->dev.parent = &client->dev;
347
348	idev->open = mpu3050_input_open;
349	idev->close = mpu3050_input_close;
350
351	__set_bit(EV_ABS, idev->evbit);
352	input_set_abs_params(idev, ABS_X,
353			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
354	input_set_abs_params(idev, ABS_Y,
355			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
356	input_set_abs_params(idev, ABS_Z,
357			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
358
359	input_set_drvdata(idev, sensor);
360
361	pm_runtime_set_active(&client->dev);
362
363	error = mpu3050_hw_init(sensor);
364	if (error)
365		goto err_pm_set_suspended;
366
367	error = request_threaded_irq(client->irq,
368				     NULL, mpu3050_interrupt_thread,
369				     IRQF_TRIGGER_RISING | IRQF_ONESHOT,
370				     "mpu3050", sensor);
371	if (error) {
372		dev_err(&client->dev,
373			"can't get IRQ %d, error %d\n", client->irq, error);
374		goto err_pm_set_suspended;
375	}
376
377	error = input_register_device(idev);
378	if (error) {
379		dev_err(&client->dev, "failed to register input device\n");
380		goto err_free_irq;
381	}
382
383	pm_runtime_enable(&client->dev);
384	pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
385	i2c_set_clientdata(client, sensor);
386
387	return 0;
388
389err_free_irq:
390	free_irq(client->irq, sensor);
391err_pm_set_suspended:
392	pm_runtime_set_suspended(&client->dev);
393err_free_mem:
394	input_free_device(idev);
395	kfree(sensor);
396	return error;
397}
398
399/**
400 *	mpu3050_remove	-	remove a sensor
401 *	@client: i2c client of sensor being removed
402 *
403 *	Our sensor is going away, clean up the resources.
404 */
405static int mpu3050_remove(struct i2c_client *client)
406{
407	struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
408
409	pm_runtime_disable(&client->dev);
410	pm_runtime_set_suspended(&client->dev);
411
412	free_irq(client->irq, sensor);
413	input_unregister_device(sensor->idev);
414	kfree(sensor);
415
416	return 0;
417}
418
419#ifdef CONFIG_PM
420/**
421 *	mpu3050_suspend		-	called on device suspend
422 *	@dev: device being suspended
423 *
424 *	Put the device into sleep mode before we suspend the machine.
425 */
426static int mpu3050_suspend(struct device *dev)
427{
428	struct i2c_client *client = to_i2c_client(dev);
429
430	mpu3050_set_power_mode(client, 0);
431
432	return 0;
433}
434
435/**
436 *	mpu3050_resume		-	called on device resume
437 *	@dev: device being resumed
438 *
439 *	Put the device into powered mode on resume.
440 */
441static int mpu3050_resume(struct device *dev)
442{
443	struct i2c_client *client = to_i2c_client(dev);
444
445	mpu3050_set_power_mode(client, 1);
446	msleep(100);  /* wait for gyro chip resume */
447
448	return 0;
449}
450#endif
451
452static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
453
454static const struct i2c_device_id mpu3050_ids[] = {
455	{ "mpu3050", 0 },
456	{ }
457};
458MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
459
460static const struct of_device_id mpu3050_of_match[] = {
461	{ .compatible = "invn,mpu3050", },
462	{ },
463};
464MODULE_DEVICE_TABLE(of, mpu3050_of_match);
465
466static struct i2c_driver mpu3050_i2c_driver = {
467	.driver	= {
468		.name	= "mpu3050",
469		.pm	= &mpu3050_pm,
470		.of_match_table = mpu3050_of_match,
471	},
472	.probe		= mpu3050_probe,
473	.remove		= mpu3050_remove,
474	.id_table	= mpu3050_ids,
475};
476
477module_i2c_driver(mpu3050_i2c_driver);
478
479MODULE_AUTHOR("Wistron Corp.");
480MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
481MODULE_LICENSE("GPL");