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/spi/spi.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"
40 #define INV_SPI_READ 0x80
41 static int inv_spi_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
43 struct spi_message msg;
46 struct spi_device *spi = to_spi_device(st->dev);
47 struct spi_transfer xfers[] = {
63 d[0] = (reg | INV_SPI_READ);
65 if ((reg == REG_FIFO_R_W) || (reg == FIFO_COUNT_BYTE))
66 spi->max_speed_hz = 20000000;
68 spi->max_speed_hz = 1000000;
70 spi_message_init(&msg);
71 spi_message_add_tail(&xfers[0], &msg);
72 spi_message_add_tail(&xfers[1], &msg);
73 res = spi_sync(spi, &msg);
78 static int inv_spi_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
80 struct spi_message msg;
83 struct spi_device *spi = to_spi_device(st->dev);
84 struct spi_transfer xfers = {
92 spi->max_speed_hz = 1000000;
93 spi_message_init(&msg);
94 spi_message_add_tail(&xfers, &msg);
95 res = spi_sync(to_spi_device(st->dev), &msg);
100 static int inv_spi_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
105 static int inv_spi_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
110 static int mpu_spi_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
111 u32 len, u8 const *data)
113 struct spi_message msg;
117 struct spi_transfer xfers = {
126 if (len > (sizeof(buf) - 1))
129 inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
130 inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
133 memcpy(buf + 1, data, len);
134 spi_message_init(&msg);
135 spi_message_add_tail(&xfers, &msg);
136 res = spi_sync(to_spi_device(st->dev), &msg);
141 static int mpu_spi_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
149 res = inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
150 res = inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
151 res = inv_plat_read(st, REG_MEM_RW, len, data);
156 #include <linux/gpio.h>
157 #include <linux/of_gpio.h>
158 #include <linux/of.h>
160 static struct mpu_platform_data mpu_data = {
169 .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
170 .sec_slave_id = COMPASS_ID_AK8963,
171 .secondary_i2c_addr = 0x0d,
172 .secondary_orientation = {
178 221, 22, 205, 7, 217, 186, 151, 55,
179 206, 254, 35, 144, 225, 102, 47, 50,
184 static int of_inv_parse_platform_data(struct spi_device *spi,
185 struct mpu_platform_data *pdata)
188 int length = 0, size = 0;
189 struct property *prop;
191 int orig_x, orig_y, orig_z;
193 struct device_node *np = spi->dev.of_node;
194 unsigned long irq_flags;
199 gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
200 gpio_request(gpio_pin, "mpu6500");
201 irq_pin = gpio_to_irq(gpio_pin);
204 ret = of_property_read_u8(np, "mpu-int_config", &mpu_data.int_config);
206 dev_err(&spi->dev, "get mpu-int_config error\n");
210 ret = of_property_read_u8(np, "mpu-level_shifter", &mpu_data.level_shifter);
212 dev_err(&spi->dev, "get mpu-level_shifter error\n");
216 prop = of_find_property(np, "mpu-orientation", &length);
218 dev_err(&spi->dev, "get mpu-orientation length error\n");
221 size = length / sizeof(u32);
222 if ((size > 0) && (size < 10)) {
223 ret = of_property_read_u32_array(np, "mpu-orientation", orientation, size);
225 dev_err(&spi->dev, "get mpu-orientation data error\n");
228 for (i = 0; i < 9; i++)
229 mpu_data.orientation[i] = orientation[i];
231 dev_info(&spi->dev, "use default orientation\n");
234 ret = of_property_read_u32(np, "orientation-x", &orig_x);
236 dev_err(&spi->dev, "get orientation-x error\n");
240 for (i = 0; i < 3; i++)
241 if (mpu_data.orientation[i])
242 mpu_data.orientation[i] = -1;
245 ret = of_property_read_u32(np, "orientation-y", &orig_y);
247 dev_err(&spi->dev, "get orientation-y error\n");
251 for (i = 3; i < 6; i++)
252 if (mpu_data.orientation[i])
253 mpu_data.orientation[i] = -1;
256 ret = of_property_read_u32(np, "orientation-z", &orig_z);
258 dev_err(&spi->dev, "get orientation-z error\n");
262 for (i = 6; i < 9; i++)
263 if (mpu_data.orientation[i])
264 mpu_data.orientation[i] = -1;
267 ret = of_property_read_u32(np, "mpu-debug", &debug);
269 dev_err(&spi->dev, "get mpu-debug error\n");
273 dev_info(&spi->dev, "int_config=%d,level_shifter=%d,irq=%x\n",
275 mpu_data.level_shifter,
277 for (i = 0; i < size; i++)
278 dev_info(&spi->dev, "%d ", mpu_data.orientation[i]);
279 dev_info(&spi->dev, "\n");
286 * inv_mpu_probe() - probe function.
288 static int inv_mpu_probe(struct spi_device *spi)
290 struct inv_mpu_iio_s *st;
291 struct iio_dev *indio_dev;
297 spi->bits_per_word = 8;
298 result = spi_setup(spi);
300 dev_err(&spi->dev, "ERR: fail to setup spi\n");
304 #ifdef INV_KERNEL_3_10
305 indio_dev = iio_device_alloc(sizeof(*st));
307 indio_dev = iio_allocate_device(sizeof(*st));
309 if (indio_dev == NULL) {
310 pr_err("memory allocation failed\n");
314 st = iio_priv(indio_dev);
315 if (spi->dev.of_node) {
316 result = of_inv_parse_platform_data(spi, &st->plat_data);
319 st->plat_data = mpu_data;
320 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
323 *(struct mpu_platform_data *)dev_get_platdata(&spi->dev);
325 /* Make state variables available to all _show and _store functions. */
326 spi_set_drvdata(spi, indio_dev);
327 indio_dev->dev.parent = &spi->dev;
330 st->i2c_dis = BIT_I2C_IF_DIS;
331 if (!strcmp(spi->modalias, "mpu6xxx"))
332 indio_dev->name = st->name;
334 indio_dev->name = spi->modalias;
336 st->plat_read = inv_spi_read;
337 st->plat_single_write = inv_spi_single_write;
338 st->secondary_read = inv_spi_secondary_read;
339 st->secondary_write = inv_spi_secondary_write;
340 st->memory_read = mpu_spi_memory_read;
341 st->memory_write = mpu_spi_memory_write;
343 /* power is turned on inside check chip type*/
344 result = inv_check_chip_type(st, indio_dev->name);
348 result = st->init_config(indio_dev);
351 "Could not initialize device.\n");
354 result = st->set_power_state(st, false);
357 "%s could not be turned off.\n", st->hw->name);
361 inv_set_iio_info(st, indio_dev);
363 result = inv_mpu_configure_ring(indio_dev);
365 pr_err("configure ring buffer fail\n");
369 result = inv_mpu_probe_trigger(indio_dev);
371 pr_err("trigger probe fail\n");
375 result = iio_device_register(indio_dev);
377 pr_err("IIO device register fail\n");
378 goto out_remove_trigger;
381 if (INV_MPU6050 == st->chip_type ||
382 INV_MPU6500 == st->chip_type) {
383 result = inv_create_dmp_sysfs(indio_dev);
385 pr_err("create dmp sysfs failed\n");
390 INIT_KFIFO(st->timestamps);
391 spin_lock_init(&st->time_stamp_lock);
392 dev_info(&spi->dev, "%s is ready to go!\n",
397 iio_device_unregister(indio_dev);
399 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
400 inv_mpu_remove_trigger(indio_dev);
402 inv_mpu_unconfigure_ring(indio_dev);
404 #ifdef INV_KERNEL_3_10
405 iio_device_free(indio_dev);
407 iio_free_device(indio_dev);
410 dev_err(&spi->dev, "%s failed %d\n", __func__, result);
415 static void inv_mpu_shutdown(struct spi_device *spi)
417 struct iio_dev *indio_dev = spi_get_drvdata(spi);
418 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
419 struct inv_reg_map_s *reg;
423 dev_dbg(&spi->dev, "Shutting down %s...\n", st->hw->name);
425 /* reset to make sure previous state are not there */
426 result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
428 dev_err(&spi->dev, "Failed to reset %s\n",
430 msleep(POWER_UP_TIME);
431 /* turn off power to ensure gyro engine is off */
432 result = st->set_power_state(st, false);
434 dev_err(&spi->dev, "Failed to turn off %s\n",
439 * inv_mpu_remove() - remove function.
441 static int inv_mpu_remove(struct spi_device *spi)
443 struct iio_dev *indio_dev = spi_get_drvdata(spi);
444 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
445 kfifo_free(&st->timestamps);
446 iio_device_unregister(indio_dev);
447 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
448 inv_mpu_remove_trigger(indio_dev);
449 inv_mpu_unconfigure_ring(indio_dev);
450 #ifdef INV_KERNEL_3_10
451 iio_device_free(indio_dev);
453 iio_free_device(indio_dev);
456 dev_info(&spi->dev, "inv-mpu-iio module removed.\n");
462 static int inv_mpu_resume(struct device *dev)
464 struct inv_mpu_iio_s *st =
465 iio_priv(spi_get_drvdata(to_spi_device(dev)));
466 pr_debug("%s inv_mpu_resume\n", st->hw->name);
467 return st->set_power_state(st, true);
470 static int inv_mpu_suspend(struct device *dev)
472 struct iio_dev *indio_dev = spi_get_drvdata(to_spi_device(dev));
473 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
476 pr_debug("%s inv_mpu_suspend\n", st->hw->name);
477 mutex_lock(&indio_dev->mlock);
479 if ((!st->chip_config.dmp_on) ||
480 (!st->chip_config.enable) ||
481 (!st->chip_config.dmp_event_int_on))
482 result = st->set_power_state(st, false);
483 mutex_unlock(&indio_dev->mlock);
487 static const struct dev_pm_ops inv_mpu_pmops = {
488 SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
490 #define INV_MPU_PMOPS (&inv_mpu_pmops)
492 #define INV_MPU_PMOPS NULL
493 #endif /* CONFIG_PM */
495 /* device id table is used to identify what device can be
496 * supported by this driver
498 static const struct spi_device_id inv_mpu_id[] = {
499 {"itg3500", INV_ITG3500},
500 {"mpu3050", INV_MPU3050},
501 {"mpu6050", INV_MPU6050},
502 {"mpu9150", INV_MPU9150},
503 {"mpu6500", INV_MPU6500},
504 {"mpu9250", INV_MPU9250},
505 {"mpu6xxx", INV_MPU6XXX},
506 {"mpu6515", INV_MPU6515},
510 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
512 static const struct of_device_id inv_mpu_of_match[] = {
513 { .compatible = "inv-spi,itg3500", },
514 { .compatible = "inv-spi,mpu3050", },
515 { .compatible = "inv-spi,mpu6050", },
516 { .compatible = "inv-spi,mpu9150", },
517 { .compatible = "inv-spi,mpu6500", },
518 { .compatible = "inv-spi,mpu9250", },
519 { .compatible = "inv-spi,mpu6xxx", },
520 { .compatible = "inv-spi,mpu9350", },
521 { .compatible = "inv-spi,mpu6515", },
525 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
527 static struct spi_driver inv_mpu_driver_spi = {
529 .owner = THIS_MODULE,
530 .name = "inv_mpu_iio_spi",
532 .of_match_table = of_match_ptr(inv_mpu_of_match),
534 .probe = inv_mpu_probe,
535 .remove = inv_mpu_remove,
536 .id_table = inv_mpu_id,
537 .shutdown = inv_mpu_shutdown,
540 static int __init inv_mpu_init(void)
542 int result = spi_register_driver(&inv_mpu_driver_spi);
551 static void __exit inv_mpu_exit(void)
553 spi_unregister_driver(&inv_mpu_driver_spi);
556 module_init(inv_mpu_init);
557 module_exit(inv_mpu_exit);
559 MODULE_AUTHOR("Invensense Corporation");
560 MODULE_DESCRIPTION("Invensense device driver");
561 MODULE_LICENSE("GPL");
562 MODULE_ALIAS("inv-mpu-iio-spi");