2 * Copyright (C) 2012 Invensense, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 * @brief Hardware drivers.
20 * @file inv_mpu_core.c
21 * @brief A sysfs device driver for Invensense devices
22 * @details This driver currently works for the
23 * MPU3050/MPU6050/MPU9150/MPU6500/MPU9250 devices.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/err.h>
32 #include <linux/delay.h>
33 #include <linux/sysfs.h>
34 #include <linux/jiffies.h>
35 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/kfifo.h>
38 #include <linux/poll.h>
39 #include <linux/miscdevice.h>
40 #include <linux/spinlock.h>
42 #include "inv_mpu_iio.h"
43 #ifdef INV_KERNEL_3_10
44 #include <linux/iio/sysfs.h>
48 #include "inv_counters.h"
51 * inv_i2c_read_base() - Read one or more bytes from the device registers.
52 * @st: Device driver instance.
53 * @reg: First device register to be read from.
54 * @length: Number of bytes to read.
55 * @data: Data read from device.
56 * NOTE:This is not re-implementation of i2c_smbus_read because i2c
57 * address could be specified in this case. We could have two different
58 * i2c address due to secondary i2c interface.
60 int inv_i2c_read_base(struct inv_mpu_iio_s *st, u16 i2c_addr,
61 u8 reg, u16 length, u8 *data)
63 struct i2c_msg msgs[2];
69 msgs[0].addr = i2c_addr;
70 msgs[0].flags = 0; /* write */
73 /* msgs[0].scl_rate = 200*1000; */
75 msgs[1].addr = i2c_addr;
76 msgs[1].flags = I2C_M_RD;
79 /* msgs[1].scl_rate = 200*1000; */
81 res = i2c_transfer(st->sl_handle, msgs, 2);
89 INV_I2C_INC_MPUWRITE(3);
90 INV_I2C_INC_MPUREAD(length);
93 pr_debug("%s RD%02X%02X%02X -> %s%s\n", st->hw->name,
94 i2c_addr, reg, length,
95 wr_pr_debug_begin(data, length, read),
96 wr_pr_debug_end(read));
102 * inv_i2c_single_write_base() - Write a byte to a device register.
103 * @st: Device driver instance.
104 * @reg: Device register to be written to.
105 * @data: Byte to write to device.
106 * NOTE:This is not re-implementation of i2c_smbus_write because i2c
107 * address could be specified in this case. We could have two different
108 * i2c address due to secondary i2c interface.
110 int inv_i2c_single_write_base(struct inv_mpu_iio_s *st,
111 u16 i2c_addr, u8 reg, u8 data)
120 msg.flags = 0; /* write */
123 /* msg.scl_rate = 200*1000; */
125 pr_debug("%s WR%02X%02X%02X\n", st->hw->name, i2c_addr, reg, data);
126 INV_I2C_INC_MPUWRITE(3);
128 res = i2c_transfer(st->sl_handle, &msg, 1);
137 int inv_plat_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
139 return inv_i2c_single_write_base(st, st->i2c_addr, reg, data);
142 int inv_plat_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
144 return inv_i2c_read_base(st, st->i2c_addr, reg, len, data);
147 int inv_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
149 return inv_i2c_read_base(st, st->plat_data.secondary_i2c_addr, reg, len, data);
152 int inv_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
154 return inv_i2c_single_write_base(st, st->plat_data.secondary_i2c_addr, reg, data);
157 int mpu_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
158 u32 len, u8 const *data)
170 if (len >= (sizeof(buf) - 1))
173 bank[0] = REG_BANK_SEL;
174 bank[1] = mem_addr >> 8;
176 addr[0] = REG_MEM_START_ADDR;
177 addr[1] = mem_addr & 0xFF;
180 memcpy(buf + 1, data, len);
186 msg.len = sizeof(bank);
187 /* msg.scl_rate = 200*1000; */
188 INV_I2C_INC_MPUWRITE(3);
189 res = i2c_transfer(st->sl_handle, &msg, 1);
199 msg.len = sizeof(addr);
200 /* msg.scl_rate = 200*1000; */
201 INV_I2C_INC_MPUWRITE(3);
202 res = i2c_transfer(st->sl_handle, &msg, 1);
213 /* msg.scl_rate = 200*1000; */
214 INV_I2C_INC_MPUWRITE(2+len);
215 res = i2c_transfer(st->sl_handle, &msg, 1);
224 pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
225 mpu_addr, bank[1], addr[1],
226 wr_pr_debug_begin(data, len, write),
227 wr_pr_debug_end(write),
233 int mpu_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
246 bank[0] = REG_BANK_SEL;
247 bank[1] = mem_addr >> 8;
249 addr[0] = REG_MEM_START_ADDR;
250 addr[1] = mem_addr & 0xFF;
258 msg.len = sizeof(bank);
259 /* msg.scl_rate = 200*1000; */
260 INV_I2C_INC_MPUWRITE(3);
261 res = i2c_transfer(st->sl_handle, &msg, 1);
271 msg.len = sizeof(addr);
272 /* msg.scl_rate = 200*1000; */
273 INV_I2C_INC_MPUWRITE(3);
274 res = i2c_transfer(st->sl_handle, &msg, 1);
285 /* msg.scl_rate = 200*1000; */
286 INV_I2C_INC_MPUWRITE(3);
287 res = i2c_transfer(st->sl_handle, &msg, 1);
295 msg.flags = I2C_M_RD;
298 /* msg.scl_rate = 200*1000; */
299 INV_I2C_INC_MPUREAD(len);
300 res = i2c_transfer(st->sl_handle, &msg, 1);
308 pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
309 mpu_addr, bank[1], addr[1], len,
310 wr_pr_debug_begin(data, len, read),
311 wr_pr_debug_end(read));
317 #include <linux/gpio.h>
318 #include <linux/of_gpio.h>
319 #include <linux/of.h>
321 static struct mpu_platform_data mpu_data = {
331 221, 22, 205, 7, 217, 186, 151, 55,
332 206, 254, 35, 144, 225, 102, 47, 50,
337 static int of_inv_parse_platform_data(struct i2c_client *client,
338 struct mpu_platform_data *pdata)
340 struct device_node *np = client->dev.of_node;
341 unsigned long irq_flags;
345 gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
346 gpio_request(gpio_pin, "mpu6500");
347 irq_pin = gpio_to_irq(gpio_pin);
348 client->irq = irq_pin;
349 i2c_set_clientdata(client, &mpu_data);
351 pr_info("%s: %s, %x, %x\n", __func__, client->name, client->addr, client->irq);
357 * inv_mpu_probe() - probe function.
359 static int inv_mpu_probe(struct i2c_client *client,
360 const struct i2c_device_id *id)
362 struct inv_mpu_iio_s *st;
363 struct iio_dev *indio_dev;
366 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
368 pr_err("I2c function error\n");
371 #ifdef INV_KERNEL_3_10
372 indio_dev = iio_device_alloc(sizeof(*st));
374 indio_dev = iio_allocate_device(sizeof(*st));
376 if (indio_dev == NULL) {
377 pr_err("memory allocation failed\n");
381 st = iio_priv(indio_dev);
383 st->sl_handle = client->adapter;
384 st->i2c_addr = client->addr;
385 if (client->dev.of_node) {
386 result = of_inv_parse_platform_data(client, &st->plat_data);
389 st->plat_data = mpu_data;
390 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
393 *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
394 /* power is turned on inside check chip type*/
395 result = inv_check_chip_type(st, id->name);
399 result = st->init_config(indio_dev);
401 dev_err(&client->adapter->dev,
402 "Could not initialize device.\n");
405 result = st->set_power_state(st, false);
407 dev_err(&client->adapter->dev,
408 "%s could not be turned off.\n", st->hw->name);
412 /* Make state variables available to all _show and _store functions. */
413 i2c_set_clientdata(client, indio_dev);
414 indio_dev->dev.parent = &client->dev;
415 if (!strcmp(id->name, "mpu6xxx"))
416 indio_dev->name = st->name;
418 indio_dev->name = id->name;
420 inv_set_iio_info(st, indio_dev);
422 result = inv_mpu_configure_ring(indio_dev);
424 pr_err("configure ring buffer fail\n");
427 st->irq = client->irq;
428 result = inv_mpu_probe_trigger(indio_dev);
430 pr_err("trigger probe fail\n");
434 /* Tell the i2c counter, we have an IRQ */
435 INV_I2C_SETIRQ(IRQ_MPU, client->irq);
437 result = iio_device_register(indio_dev);
439 pr_err("IIO device register fail\n");
440 goto out_remove_trigger;
443 if (INV_MPU6050 == st->chip_type ||
444 INV_MPU6500 == st->chip_type) {
445 result = inv_create_dmp_sysfs(indio_dev);
447 pr_err("create dmp sysfs failed\n");
452 INIT_KFIFO(st->timestamps);
453 spin_lock_init(&st->time_stamp_lock);
454 dev_info(&client->dev, "%s is ready to go!\n",
459 iio_device_unregister(indio_dev);
461 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
462 inv_mpu_remove_trigger(indio_dev);
464 inv_mpu_unconfigure_ring(indio_dev);
466 #ifdef INV_KERNEL_3_10
467 iio_device_free(indio_dev);
469 iio_free_device(indio_dev);
472 dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
477 static void inv_mpu_shutdown(struct i2c_client *client)
479 struct iio_dev *indio_dev = i2c_get_clientdata(client);
480 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
481 struct inv_reg_map_s *reg;
485 dev_dbg(&client->adapter->dev, "Shutting down %s...\n", st->hw->name);
487 /* reset to make sure previous state are not there */
488 result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
490 dev_err(&client->adapter->dev, "Failed to reset %s\n",
492 msleep(POWER_UP_TIME);
493 /* turn off power to ensure gyro engine is off */
494 result = st->set_power_state(st, false);
496 dev_err(&client->adapter->dev, "Failed to turn off %s\n",
501 * inv_mpu_remove() - remove function.
503 static int inv_mpu_remove(struct i2c_client *client)
505 struct iio_dev *indio_dev = i2c_get_clientdata(client);
506 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
507 kfifo_free(&st->timestamps);
508 iio_device_unregister(indio_dev);
509 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
510 inv_mpu_remove_trigger(indio_dev);
511 inv_mpu_unconfigure_ring(indio_dev);
512 #ifdef INV_KERNEL_3_10
513 iio_device_free(indio_dev);
515 iio_free_device(indio_dev);
518 dev_info(&client->adapter->dev, "inv-mpu-iio module removed.\n");
524 static int inv_mpu_resume(struct device *dev)
526 struct inv_mpu_iio_s *st =
527 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
528 pr_debug("%s inv_mpu_resume\n", st->hw->name);
529 return st->set_power_state(st, true);
532 static int inv_mpu_suspend(struct device *dev)
534 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
535 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
538 pr_debug("%s inv_mpu_suspend\n", st->hw->name);
539 mutex_lock(&indio_dev->mlock);
541 if ((!st->chip_config.dmp_on) ||
542 (!st->chip_config.enable) ||
543 (!st->chip_config.dmp_event_int_on))
544 result = st->set_power_state(st, false);
545 mutex_unlock(&indio_dev->mlock);
549 static const struct dev_pm_ops inv_mpu_pmops = {
550 SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
552 #define INV_MPU_PMOPS (&inv_mpu_pmops)
554 #define INV_MPU_PMOPS NULL
555 #endif /* CONFIG_PM */
557 static const u16 normal_i2c[] = { I2C_CLIENT_END };
558 /* device id table is used to identify what device can be
559 * supported by this driver
561 static const struct i2c_device_id inv_mpu_id[] = {
562 {"itg3500", INV_ITG3500},
563 {"mpu3050", INV_MPU3050},
564 {"mpu6050", INV_MPU6050},
565 {"mpu9150", INV_MPU9150},
566 {"mpu6500", INV_MPU6500},
567 {"mpu9250", INV_MPU9250},
568 {"mpu6xxx", INV_MPU6XXX},
569 {"mpu6515", INV_MPU6515},
573 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
575 static const struct of_device_id inv_mpu_of_match[] = {
576 { .compatible = "invensense,itg3500", },
577 { .compatible = "invensense,mpu3050", },
578 { .compatible = "invensense,mpu6050", },
579 { .compatible = "invensense,mpu9150", },
580 { .compatible = "invensense,mpu6500", },
581 { .compatible = "invensense,mpu9250", },
582 { .compatible = "invensense,mpu6xxx", },
583 { .compatible = "invensense,mpu9350", },
584 { .compatible = "invensense,mpu6515", },
588 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
590 static struct i2c_driver inv_mpu_driver = {
591 .class = I2C_CLASS_HWMON,
592 .probe = inv_mpu_probe,
593 .remove = inv_mpu_remove,
594 .shutdown = inv_mpu_shutdown,
595 .id_table = inv_mpu_id,
597 .owner = THIS_MODULE,
598 .name = "inv-mpu-iio",
600 .of_match_table = of_match_ptr(inv_mpu_of_match),
602 .address_list = normal_i2c,
605 static int __init inv_mpu_init(void)
607 int result = i2c_add_driver(&inv_mpu_driver);
608 pr_info("%s:%d\n", __func__, __LINE__);
616 static void __exit inv_mpu_exit(void)
618 i2c_del_driver(&inv_mpu_driver);
621 module_init(inv_mpu_init);
622 module_exit(inv_mpu_exit);
624 MODULE_AUTHOR("Invensense Corporation");
625 MODULE_DESCRIPTION("Invensense device driver");
626 MODULE_LICENSE("GPL");
627 MODULE_ALIAS("inv-mpu-iio");