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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/i2c.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
23 #include <linux/sysfs.h>
24 #include <linux/jiffies.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/kfifo.h>
28 #include <linux/poll.h>
29 #include <linux/miscdevice.h>
30 #include <linux/spinlock.h>
32 #include "inv_mpu_iio.h"
33 #ifdef INV_KERNEL_3_10
34 #include <linux/iio/sysfs.h>
38 #include "inv_counters.h"
41 * inv_i2c_read_base() - Read one or more bytes from the device registers.
42 * @st: Device driver instance.
43 * @reg: First device register to be read from.
44 * @length: Number of bytes to read.
45 * @data: Data read from device.
46 * NOTE:This is not re-implementation of i2c_smbus_read because i2c
47 * address could be specified in this case. We could have two different
48 * i2c address due to secondary i2c interface.
50 static int inv_i2c_read_base(struct inv_mpu_iio_s *st, u16 i2c_addr,
51 u8 reg, u16 length, u8 *data)
53 struct i2c_msg msgs[2];
59 msgs[0].addr = i2c_addr;
60 msgs[0].flags = 0; /* write */
63 /* msgs[0].scl_rate = 200*1000; */
65 msgs[1].addr = i2c_addr;
66 msgs[1].flags = I2C_M_RD;
69 /* msgs[1].scl_rate = 200*1000; */
71 res = i2c_transfer(st->sl_handle, msgs, 2);
79 INV_I2C_INC_MPUWRITE(3);
80 INV_I2C_INC_MPUREAD(length);
83 pr_debug("%s RD%02X%02X%02X -> %s%s\n", st->hw->name,
84 i2c_addr, reg, length,
85 wr_pr_debug_begin(data, length, read),
86 wr_pr_debug_end(read));
92 * inv_i2c_single_write_base() - Write a byte to a device register.
93 * @st: Device driver instance.
94 * @reg: Device register to be written to.
95 * @data: Byte to write to device.
96 * NOTE:This is not re-implementation of i2c_smbus_write because i2c
97 * address could be specified in this case. We could have two different
98 * i2c address due to secondary i2c interface.
100 static int inv_i2c_single_write_base(struct inv_mpu_iio_s *st,
101 u16 i2c_addr, u8 reg, u8 data)
110 msg.flags = 0; /* write */
113 /* msg.scl_rate = 200*1000; */
115 pr_debug("%s WR%02X%02X%02X\n", st->hw->name, i2c_addr, reg, data);
116 INV_I2C_INC_MPUWRITE(3);
118 res = i2c_transfer(st->sl_handle, &msg, 1);
127 static int inv_i2c_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
129 return inv_i2c_single_write_base(st, st->i2c_addr, reg, data);
132 static int inv_i2c_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
134 return inv_i2c_read_base(st, st->i2c_addr, reg, len, data);
137 static int inv_i2c_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
139 return inv_i2c_read_base(st, st->plat_data.secondary_i2c_addr, reg, len, data);
142 static int inv_i2c_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
144 return inv_i2c_single_write_base(st, st->plat_data.secondary_i2c_addr, reg, data);
147 static int mpu_i2c_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
148 u32 len, u8 const *data)
160 if (len >= (sizeof(buf) - 1))
163 bank[0] = REG_BANK_SEL;
164 bank[1] = mem_addr >> 8;
166 addr[0] = REG_MEM_START_ADDR;
167 addr[1] = mem_addr & 0xFF;
170 memcpy(buf + 1, data, len);
176 msg.len = sizeof(bank);
177 /* msg.scl_rate = 200*1000; */
178 INV_I2C_INC_MPUWRITE(3);
179 res = i2c_transfer(st->sl_handle, &msg, 1);
189 msg.len = sizeof(addr);
190 /* msg.scl_rate = 200*1000; */
191 INV_I2C_INC_MPUWRITE(3);
192 res = i2c_transfer(st->sl_handle, &msg, 1);
203 /* msg.scl_rate = 200*1000; */
204 INV_I2C_INC_MPUWRITE(2+len);
205 res = i2c_transfer(st->sl_handle, &msg, 1);
214 pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
215 mpu_addr, bank[1], addr[1],
216 wr_pr_debug_begin(data, len, write),
217 wr_pr_debug_end(write),
223 static int mpu_i2c_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
236 bank[0] = REG_BANK_SEL;
237 bank[1] = mem_addr >> 8;
239 addr[0] = REG_MEM_START_ADDR;
240 addr[1] = mem_addr & 0xFF;
248 msg.len = sizeof(bank);
249 /* msg.scl_rate = 200*1000; */
250 INV_I2C_INC_MPUWRITE(3);
251 res = i2c_transfer(st->sl_handle, &msg, 1);
261 msg.len = sizeof(addr);
262 /* msg.scl_rate = 200*1000; */
263 INV_I2C_INC_MPUWRITE(3);
264 res = i2c_transfer(st->sl_handle, &msg, 1);
275 /* msg.scl_rate = 200*1000; */
276 INV_I2C_INC_MPUWRITE(3);
277 res = i2c_transfer(st->sl_handle, &msg, 1);
285 msg.flags = I2C_M_RD;
288 /* msg.scl_rate = 200*1000; */
289 INV_I2C_INC_MPUREAD(len);
290 res = i2c_transfer(st->sl_handle, &msg, 1);
298 pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
299 mpu_addr, bank[1], addr[1], len,
300 wr_pr_debug_begin(data, len, read),
301 wr_pr_debug_end(read));
307 #include <linux/gpio.h>
308 #include <linux/of_gpio.h>
309 #include <linux/of.h>
311 static struct mpu_platform_data mpu_data = {
320 .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
321 .sec_slave_id = COMPASS_ID_AK8963,
322 .secondary_i2c_addr = 0x0d,
323 .secondary_orientation = {
329 221, 22, 205, 7, 217, 186, 151, 55,
330 206, 254, 35, 144, 225, 102, 47, 50,
335 static int of_inv_parse_platform_data(struct i2c_client *client,
336 struct mpu_platform_data *pdata)
339 int length = 0, size = 0;
340 struct property *prop;
342 int orig_x, orig_y, orig_z;
344 struct device_node *np = client->dev.of_node;
345 unsigned long irq_flags;
350 gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
351 gpio_request(gpio_pin, "mpu6500");
352 irq_pin = gpio_to_irq(gpio_pin);
353 client->irq = irq_pin;
355 i2c_set_clientdata(client, &mpu_data);
357 ret = of_property_read_u8(np, "mpu-int_config", &mpu_data.int_config);
359 dev_err(&client->dev, "get mpu-int_config error\n");
363 ret = of_property_read_u8(np, "mpu-level_shifter", &mpu_data.level_shifter);
365 dev_err(&client->dev, "get mpu-level_shifter error\n");
369 prop = of_find_property(np, "mpu-orientation", &length);
371 dev_err(&client->dev, "get mpu-orientation length error\n");
374 size = length / sizeof(u32);
375 if ((size > 0) && (size < 10)) {
376 ret = of_property_read_u32_array(np, "mpu-orientation", orientation, size);
378 dev_err(&client->dev, "get mpu-orientation data error\n");
381 for (i = 0; i < 9; i++)
382 mpu_data.orientation[i] = orientation[i];
384 dev_info(&client->dev, "use default orientation\n");
387 ret = of_property_read_u32(np, "orientation-x", &orig_x);
389 dev_err(&client->dev, "get orientation-x error\n");
393 for (i = 0; i < 3; i++)
394 if (mpu_data.orientation[i])
395 mpu_data.orientation[i] = -1;
398 ret = of_property_read_u32(np, "orientation-y", &orig_y);
400 dev_err(&client->dev, "get orientation-y error\n");
404 for (i = 3; i < 6; i++)
405 if (mpu_data.orientation[i])
406 mpu_data.orientation[i] = -1;
409 ret = of_property_read_u32(np, "orientation-z", &orig_z);
411 dev_err(&client->dev, "get orientation-z error\n");
415 for (i = 6; i < 9; i++)
416 if (mpu_data.orientation[i])
417 mpu_data.orientation[i] = -1;
420 ret = of_property_read_u32(np, "mpu-debug", &debug);
422 dev_err(&client->dev, "get mpu-debug error\n");
426 dev_info(&client->dev, "int_config=%d,level_shifter=%d,client.addr=%x,client.irq=%x\n",
428 mpu_data.level_shifter,
431 for (i = 0; i < size; i++)
432 dev_info(&client->dev, "%d ", mpu_data.orientation[i]);
433 dev_info(&client->dev, "\n");
440 * inv_mpu_probe() - probe function.
442 static int inv_mpu_probe(struct i2c_client *client,
443 const struct i2c_device_id *id)
445 struct inv_mpu_iio_s *st;
446 struct iio_dev *indio_dev;
449 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
451 pr_err("I2c function error\n");
454 #ifdef INV_KERNEL_3_10
455 indio_dev = iio_device_alloc(sizeof(*st));
457 indio_dev = iio_allocate_device(sizeof(*st));
459 if (indio_dev == NULL) {
460 pr_err("memory allocation failed\n");
464 st = iio_priv(indio_dev);
466 st->sl_handle = client->adapter;
467 st->i2c_addr = client->addr;
468 if (client->dev.of_node) {
469 result = of_inv_parse_platform_data(client, &st->plat_data);
472 st->plat_data = mpu_data;
473 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
476 *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
478 st->irq = client->irq;
479 st->plat_read = inv_i2c_read;
480 st->plat_single_write = inv_i2c_single_write;
481 st->secondary_read = inv_i2c_secondary_read;
482 st->secondary_write = inv_i2c_secondary_write;
483 st->memory_read = mpu_i2c_memory_read;
484 st->memory_write = mpu_i2c_memory_write;
486 /* power is turned on inside check chip type*/
487 result = inv_check_chip_type(st, id->name);
491 result = st->init_config(indio_dev);
493 dev_err(&client->adapter->dev,
494 "Could not initialize device.\n");
497 result = st->set_power_state(st, false);
499 dev_err(&client->adapter->dev,
500 "%s could not be turned off.\n", st->hw->name);
504 /* Make state variables available to all _show and _store functions. */
505 i2c_set_clientdata(client, indio_dev);
506 indio_dev->dev.parent = &client->dev;
507 if (!strcmp(id->name, "mpu6xxx"))
508 indio_dev->name = st->name;
510 indio_dev->name = id->name;
512 inv_set_iio_info(st, indio_dev);
514 result = inv_mpu_configure_ring(indio_dev);
516 pr_err("configure ring buffer fail\n");
519 st->dev = &client->dev;
520 result = inv_mpu_probe_trigger(indio_dev);
522 pr_err("trigger probe fail\n");
526 /* Tell the i2c counter, we have an IRQ */
527 INV_I2C_SETIRQ(IRQ_MPU, client->irq);
529 result = iio_device_register(indio_dev);
531 pr_err("IIO device register fail\n");
532 goto out_remove_trigger;
535 if (INV_MPU6050 == st->chip_type ||
536 INV_MPU6500 == st->chip_type) {
537 result = inv_create_dmp_sysfs(indio_dev);
539 pr_err("create dmp sysfs failed\n");
544 INIT_KFIFO(st->timestamps);
545 spin_lock_init(&st->time_stamp_lock);
546 dev_info(&client->dev, "%s is ready to go!\n",
551 iio_device_unregister(indio_dev);
553 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
554 inv_mpu_remove_trigger(indio_dev);
556 inv_mpu_unconfigure_ring(indio_dev);
558 #ifdef INV_KERNEL_3_10
559 iio_device_free(indio_dev);
561 iio_free_device(indio_dev);
564 dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
569 static void inv_mpu_shutdown(struct i2c_client *client)
571 struct iio_dev *indio_dev = i2c_get_clientdata(client);
572 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
573 struct inv_reg_map_s *reg;
577 dev_dbg(&client->adapter->dev, "Shutting down %s...\n", st->hw->name);
579 /* reset to make sure previous state are not there */
580 result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
582 dev_err(&client->adapter->dev, "Failed to reset %s\n",
584 msleep(POWER_UP_TIME);
585 /* turn off power to ensure gyro engine is off */
586 result = st->set_power_state(st, false);
588 dev_err(&client->adapter->dev, "Failed to turn off %s\n",
593 * inv_mpu_remove() - remove function.
595 static int inv_mpu_remove(struct i2c_client *client)
597 struct iio_dev *indio_dev = i2c_get_clientdata(client);
598 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
599 kfifo_free(&st->timestamps);
600 iio_device_unregister(indio_dev);
601 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
602 inv_mpu_remove_trigger(indio_dev);
603 inv_mpu_unconfigure_ring(indio_dev);
604 #ifdef INV_KERNEL_3_10
605 iio_device_free(indio_dev);
607 iio_free_device(indio_dev);
610 dev_info(&client->adapter->dev, "inv-mpu-iio module removed.\n");
616 static int inv_mpu_resume(struct device *dev)
618 struct inv_mpu_iio_s *st =
619 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
620 pr_debug("%s inv_mpu_resume\n", st->hw->name);
621 return st->set_power_state(st, true);
624 static int inv_mpu_suspend(struct device *dev)
626 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
627 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
630 pr_debug("%s inv_mpu_suspend\n", st->hw->name);
631 mutex_lock(&indio_dev->mlock);
633 if ((!st->chip_config.dmp_on) ||
634 (!st->chip_config.enable) ||
635 (!st->chip_config.dmp_event_int_on))
636 result = st->set_power_state(st, false);
637 mutex_unlock(&indio_dev->mlock);
641 static const struct dev_pm_ops inv_mpu_pmops = {
642 SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
644 #define INV_MPU_PMOPS (&inv_mpu_pmops)
646 #define INV_MPU_PMOPS NULL
647 #endif /* CONFIG_PM */
649 static const u16 normal_i2c[] = { I2C_CLIENT_END };
650 /* device id table is used to identify what device can be
651 * supported by this driver
653 static const struct i2c_device_id inv_mpu_id[] = {
654 {"itg3500", INV_ITG3500},
655 {"mpu3050", INV_MPU3050},
656 {"mpu6050", INV_MPU6050},
657 {"mpu9150", INV_MPU9150},
658 {"mpu6500", INV_MPU6500},
659 {"mpu9250", INV_MPU9250},
660 {"mpu6xxx", INV_MPU6XXX},
661 {"mpu6515", INV_MPU6515},
665 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
667 static const struct of_device_id inv_mpu_of_match[] = {
668 { .compatible = "invensense,itg3500", },
669 { .compatible = "invensense,mpu3050", },
670 { .compatible = "invensense,mpu6050", },
671 { .compatible = "invensense,mpu9150", },
672 { .compatible = "invensense,mpu6500", },
673 { .compatible = "invensense,mpu9250", },
674 { .compatible = "invensense,mpu6xxx", },
675 { .compatible = "invensense,mpu9350", },
676 { .compatible = "invensense,mpu6515", },
680 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
682 static struct i2c_driver inv_mpu_driver = {
683 .class = I2C_CLASS_HWMON,
684 .probe = inv_mpu_probe,
685 .remove = inv_mpu_remove,
686 .shutdown = inv_mpu_shutdown,
687 .id_table = inv_mpu_id,
689 .owner = THIS_MODULE,
690 .name = "inv-mpu-iio",
692 .of_match_table = of_match_ptr(inv_mpu_of_match),
694 .address_list = normal_i2c,
697 static int __init inv_mpu_init(void)
699 int result = i2c_add_driver(&inv_mpu_driver);
700 pr_info("%s:%d\n", __func__, __LINE__);
708 static void __exit inv_mpu_exit(void)
710 i2c_del_driver(&inv_mpu_driver);
713 module_init(inv_mpu_init);
714 module_exit(inv_mpu_exit);
716 MODULE_AUTHOR("Invensense Corporation");
717 MODULE_DESCRIPTION("Invensense device driver");
718 MODULE_LICENSE("GPL");
719 MODULE_ALIAS("inv-mpu-iio");