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_transfer xfers[] = {
62 d[0] = (reg | INV_SPI_READ);
64 spi_message_init(&msg);
65 spi_message_add_tail(&xfers[0], &msg);
66 spi_message_add_tail(&xfers[1], &msg);
67 res = spi_sync(to_spi_device(st->dev), &msg);
72 static int inv_spi_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
74 struct spi_message msg;
77 struct spi_transfer xfers = {
85 spi_message_init(&msg);
86 spi_message_add_tail(&xfers, &msg);
87 res = spi_sync(to_spi_device(st->dev), &msg);
92 static int inv_spi_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
97 static int inv_spi_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
102 static int mpu_spi_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
103 u32 len, u8 const *data)
105 struct spi_message msg;
109 struct spi_transfer xfers = {
118 if (len > (sizeof(buf) - 1))
121 inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
122 inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
125 memcpy(buf + 1, data, len);
126 spi_message_init(&msg);
127 spi_message_add_tail(&xfers, &msg);
128 res = spi_sync(to_spi_device(st->dev), &msg);
133 static int mpu_spi_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
141 res = inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
142 res = inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
143 res = inv_plat_read(st, REG_MEM_RW, len, data);
148 #include <linux/gpio.h>
149 #include <linux/of_gpio.h>
150 #include <linux/of.h>
152 static struct mpu_platform_data mpu_data = {
161 .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
162 .sec_slave_id = COMPASS_ID_AK8963,
163 .secondary_i2c_addr = 0x0d,
164 .secondary_orientation = {
170 221, 22, 205, 7, 217, 186, 151, 55,
171 206, 254, 35, 144, 225, 102, 47, 50,
176 static int of_inv_parse_platform_data(struct spi_device *spi,
177 struct mpu_platform_data *pdata)
180 int length = 0, size = 0;
181 struct property *prop;
183 int orig_x, orig_y, orig_z;
185 struct device_node *np = spi->dev.of_node;
186 unsigned long irq_flags;
191 gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
192 gpio_request(gpio_pin, "mpu6500");
193 irq_pin = gpio_to_irq(gpio_pin);
196 ret = of_property_read_u8(np, "mpu-int_config", &mpu_data.int_config);
198 dev_err(&spi->dev, "get mpu-int_config error\n");
202 ret = of_property_read_u8(np, "mpu-level_shifter", &mpu_data.level_shifter);
204 dev_err(&spi->dev, "get mpu-level_shifter error\n");
208 prop = of_find_property(np, "mpu-orientation", &length);
210 dev_err(&spi->dev, "get mpu-orientation length error\n");
213 size = length / sizeof(u32);
214 if ((size > 0) && (size < 10)) {
215 ret = of_property_read_u32_array(np, "mpu-orientation", orientation, size);
217 dev_err(&spi->dev, "get mpu-orientation data error\n");
220 for (i = 0; i < 9; i++)
221 mpu_data.orientation[i] = orientation[i];
223 dev_info(&spi->dev, "use default orientation\n");
226 ret = of_property_read_u32(np, "orientation-x", &orig_x);
228 dev_err(&spi->dev, "get orientation-x error\n");
232 for (i = 0; i < 3; i++)
233 if (mpu_data.orientation[i])
234 mpu_data.orientation[i] = -1;
237 ret = of_property_read_u32(np, "orientation-y", &orig_y);
239 dev_err(&spi->dev, "get orientation-y error\n");
243 for (i = 3; i < 6; i++)
244 if (mpu_data.orientation[i])
245 mpu_data.orientation[i] = -1;
248 ret = of_property_read_u32(np, "orientation-z", &orig_z);
250 dev_err(&spi->dev, "get orientation-z error\n");
254 for (i = 6; i < 9; i++)
255 if (mpu_data.orientation[i])
256 mpu_data.orientation[i] = -1;
259 ret = of_property_read_u32(np, "mpu-debug", &debug);
261 dev_err(&spi->dev, "get mpu-debug error\n");
265 dev_info(&spi->dev, "int_config=%d,level_shifter=%d,irq=%x\n",
267 mpu_data.level_shifter,
269 for (i = 0; i < size; i++)
270 dev_info(&spi->dev, "%d ", mpu_data.orientation[i]);
271 dev_info(&spi->dev, "\n");
278 * inv_mpu_probe() - probe function.
280 static int inv_mpu_probe(struct spi_device *spi)
282 struct inv_mpu_iio_s *st;
283 struct iio_dev *indio_dev;
289 spi->bits_per_word = 8;
290 result = spi_setup(spi);
292 dev_err(&spi->dev, "ERR: fail to setup spi\n");
296 #ifdef INV_KERNEL_3_10
297 indio_dev = iio_device_alloc(sizeof(*st));
299 indio_dev = iio_allocate_device(sizeof(*st));
301 if (indio_dev == NULL) {
302 pr_err("memory allocation failed\n");
306 st = iio_priv(indio_dev);
307 if (spi->dev.of_node) {
308 result = of_inv_parse_platform_data(spi, &st->plat_data);
311 st->plat_data = mpu_data;
312 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
315 *(struct mpu_platform_data *)dev_get_platdata(&spi->dev);
317 /* Make state variables available to all _show and _store functions. */
318 spi_set_drvdata(spi, indio_dev);
319 indio_dev->dev.parent = &spi->dev;
322 st->i2c_dis = BIT_I2C_IF_DIS;
323 if (!strcmp(spi->modalias, "mpu6xxx"))
324 indio_dev->name = st->name;
326 indio_dev->name = spi->modalias;
328 st->plat_read = inv_spi_read;
329 st->plat_single_write = inv_spi_single_write;
330 st->secondary_read = inv_spi_secondary_read;
331 st->secondary_write = inv_spi_secondary_write;
332 st->memory_read = mpu_spi_memory_read;
333 st->memory_write = mpu_spi_memory_write;
335 /* power is turned on inside check chip type*/
336 result = inv_check_chip_type(st, indio_dev->name);
340 result = st->init_config(indio_dev);
343 "Could not initialize device.\n");
346 result = st->set_power_state(st, false);
349 "%s could not be turned off.\n", st->hw->name);
353 inv_set_iio_info(st, indio_dev);
355 result = inv_mpu_configure_ring(indio_dev);
357 pr_err("configure ring buffer fail\n");
361 result = inv_mpu_probe_trigger(indio_dev);
363 pr_err("trigger probe fail\n");
367 result = iio_device_register(indio_dev);
369 pr_err("IIO device register fail\n");
370 goto out_remove_trigger;
373 if (INV_MPU6050 == st->chip_type ||
374 INV_MPU6500 == st->chip_type) {
375 result = inv_create_dmp_sysfs(indio_dev);
377 pr_err("create dmp sysfs failed\n");
382 INIT_KFIFO(st->timestamps);
383 spin_lock_init(&st->time_stamp_lock);
384 dev_info(&spi->dev, "%s is ready to go!\n",
389 iio_device_unregister(indio_dev);
391 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
392 inv_mpu_remove_trigger(indio_dev);
394 inv_mpu_unconfigure_ring(indio_dev);
396 #ifdef INV_KERNEL_3_10
397 iio_device_free(indio_dev);
399 iio_free_device(indio_dev);
402 dev_err(&spi->dev, "%s failed %d\n", __func__, result);
407 static void inv_mpu_shutdown(struct spi_device *spi)
409 struct iio_dev *indio_dev = spi_get_drvdata(spi);
410 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
411 struct inv_reg_map_s *reg;
415 dev_dbg(&spi->dev, "Shutting down %s...\n", st->hw->name);
417 /* reset to make sure previous state are not there */
418 result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
420 dev_err(&spi->dev, "Failed to reset %s\n",
422 msleep(POWER_UP_TIME);
423 /* turn off power to ensure gyro engine is off */
424 result = st->set_power_state(st, false);
426 dev_err(&spi->dev, "Failed to turn off %s\n",
431 * inv_mpu_remove() - remove function.
433 static int inv_mpu_remove(struct spi_device *spi)
435 struct iio_dev *indio_dev = spi_get_drvdata(spi);
436 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
437 kfifo_free(&st->timestamps);
438 iio_device_unregister(indio_dev);
439 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
440 inv_mpu_remove_trigger(indio_dev);
441 inv_mpu_unconfigure_ring(indio_dev);
442 #ifdef INV_KERNEL_3_10
443 iio_device_free(indio_dev);
445 iio_free_device(indio_dev);
448 dev_info(&spi->dev, "inv-mpu-iio module removed.\n");
454 static int inv_mpu_resume(struct device *dev)
456 struct inv_mpu_iio_s *st =
457 iio_priv(spi_get_drvdata(to_spi_device(dev)));
458 pr_debug("%s inv_mpu_resume\n", st->hw->name);
459 return st->set_power_state(st, true);
462 static int inv_mpu_suspend(struct device *dev)
464 struct iio_dev *indio_dev = spi_get_drvdata(to_spi_device(dev));
465 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
468 pr_debug("%s inv_mpu_suspend\n", st->hw->name);
469 mutex_lock(&indio_dev->mlock);
471 if ((!st->chip_config.dmp_on) ||
472 (!st->chip_config.enable) ||
473 (!st->chip_config.dmp_event_int_on))
474 result = st->set_power_state(st, false);
475 mutex_unlock(&indio_dev->mlock);
479 static const struct dev_pm_ops inv_mpu_pmops = {
480 SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
482 #define INV_MPU_PMOPS (&inv_mpu_pmops)
484 #define INV_MPU_PMOPS NULL
485 #endif /* CONFIG_PM */
487 /* device id table is used to identify what device can be
488 * supported by this driver
490 static const struct spi_device_id inv_mpu_id[] = {
491 {"itg3500", INV_ITG3500},
492 {"mpu3050", INV_MPU3050},
493 {"mpu6050", INV_MPU6050},
494 {"mpu9150", INV_MPU9150},
495 {"mpu6500", INV_MPU6500},
496 {"mpu9250", INV_MPU9250},
497 {"mpu6xxx", INV_MPU6XXX},
498 {"mpu6515", INV_MPU6515},
502 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
504 static const struct of_device_id inv_mpu_of_match[] = {
505 { .compatible = "inv-spi,itg3500", },
506 { .compatible = "inv-spi,mpu3050", },
507 { .compatible = "inv-spi,mpu6050", },
508 { .compatible = "inv-spi,mpu9150", },
509 { .compatible = "inv-spi,mpu6500", },
510 { .compatible = "inv-spi,mpu9250", },
511 { .compatible = "inv-spi,mpu6xxx", },
512 { .compatible = "inv-spi,mpu9350", },
513 { .compatible = "inv-spi,mpu6515", },
517 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
519 static struct spi_driver inv_mpu_driver_spi = {
521 .owner = THIS_MODULE,
522 .name = "inv_mpu_iio_spi",
524 .of_match_table = of_match_ptr(inv_mpu_of_match),
526 .probe = inv_mpu_probe,
527 .remove = inv_mpu_remove,
528 .id_table = inv_mpu_id,
529 .shutdown = inv_mpu_shutdown,
532 static int __init inv_mpu_init(void)
534 int result = spi_register_driver(&inv_mpu_driver_spi);
543 static void __exit inv_mpu_exit(void)
545 spi_unregister_driver(&inv_mpu_driver_spi);
548 module_init(inv_mpu_init);
549 module_exit(inv_mpu_exit);
551 MODULE_AUTHOR("Invensense Corporation");
552 MODULE_DESCRIPTION("Invensense device driver");
553 MODULE_LICENSE("GPL");
554 MODULE_ALIAS("inv-mpu-iio-spi");