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.
13 #define pr_fmt(fmt) "inv_mpu: " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/sysfs.h>
22 #include <linux/jiffies.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25 #include <linux/kfifo.h>
26 #include <linux/poll.h>
27 #include <linux/miscdevice.h>
28 #include <linux/spinlock.h>
30 #include "inv_mpu_iio.h"
32 #include "inv_test/inv_counters.h"
34 #ifdef CONFIG_DTS_INV_MPU_IIO
35 #include "inv_mpu_dts.h"
39 * inv_i2c_read_base() - Read one or more bytes from the device registers.
40 * @st: Device driver instance.
41 * @i2c_addr: i2c address of device.
42 * @reg: First device register to be read from.
43 * @length: Number of bytes to read.
44 * @data: Data read from device.
45 * NOTE:This is not re-implementation of i2c_smbus_read because i2c
46 * address could be specified in this case. We could have two different
47 * i2c address due to secondary i2c interface.
49 int inv_i2c_read_base(struct inv_mpu_state *st, u16 i2c_addr,
50 u8 reg, u16 length, u8 *data)
52 struct i2c_msg msgs[2];
58 msgs[0].addr = i2c_addr;
59 msgs[0].flags = 0; /* write */
63 msgs[1].addr = i2c_addr;
64 msgs[1].flags = I2C_M_RD;
68 res = i2c_transfer(st->sl_handle, msgs, 2);
76 INV_I2C_INC_MPUWRITE(3);
77 INV_I2C_INC_MPUREAD(length);
83 * inv_i2c_single_write_base() - Write a byte to a device register.
84 * @st: Device driver instance.
85 * @i2c_addr: I2C address of the device.
86 * @reg: Device register to be written to.
87 * @data: Byte to write to device.
88 * NOTE:This is not re-implementation of i2c_smbus_write because i2c
89 * address could be specified in this case. We could have two different
90 * i2c address due to secondary i2c interface.
92 int inv_i2c_single_write_base(struct inv_mpu_state *st,
93 u16 i2c_addr, u8 reg, u8 data)
103 msg.flags = 0; /* write */
107 INV_I2C_INC_MPUWRITE(3);
109 res = i2c_transfer(st->sl_handle, &msg, 1);
118 int inv_plat_single_write(struct inv_mpu_state *st, u8 reg, u8 data)
120 return inv_i2c_single_write_base(st, st->i2c_addr, reg, data);
122 int inv_plat_read(struct inv_mpu_state *st, u8 reg, int len, u8 *data)
124 return inv_i2c_read_base(st, st->i2c_addr, reg, len, data);
126 int mpu_memory_write(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
127 u32 len, u8 const *data)
133 struct i2c_msg msgs[3];
139 if (len >= (sizeof(buf) - 1))
142 bank[0] = REG_MEM_BANK_SEL;
143 bank[1] = mem_addr >> 8;
145 addr[0] = REG_MEM_START_ADDR;
146 addr[1] = mem_addr & 0xFF;
148 buf[0] = REG_MEM_R_W;
149 memcpy(buf + 1, data, len);
152 msgs[0].addr = mpu_addr;
155 msgs[0].len = sizeof(bank);
157 msgs[1].addr = mpu_addr;
160 msgs[1].len = sizeof(addr);
162 msgs[2].addr = mpu_addr;
164 msgs[2].buf = (u8 *)buf;
165 msgs[2].len = len + 1;
167 INV_I2C_INC_MPUWRITE(3 + 3 + (2 + len));
168 #if CONFIG_DYNAMIC_DEBUG
171 pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
172 mpu_addr, bank[1], addr[1],
173 wr_pr_debug_begin(data, len, write),
174 wr_pr_debug_end(write),
179 res = i2c_transfer(st->sl_handle, msgs, 3);
189 int mpu_memory_read(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
196 struct i2c_msg msgs[4];
202 bank[0] = REG_MEM_BANK_SEL;
203 bank[1] = mem_addr >> 8;
205 addr[0] = REG_MEM_START_ADDR;
206 addr[1] = mem_addr & 0xFF;
211 msgs[0].addr = mpu_addr;
214 msgs[0].len = sizeof(bank);
216 msgs[1].addr = mpu_addr;
219 msgs[1].len = sizeof(addr);
221 msgs[2].addr = mpu_addr;
226 msgs[3].addr = mpu_addr;
227 msgs[3].flags = I2C_M_RD;
231 res = i2c_transfer(st->sl_handle, msgs, 4);
238 INV_I2C_INC_MPUWRITE(3 + 3 + 3);
239 INV_I2C_INC_MPUREAD(len);
240 #if CONFIG_DYNAMIC_DEBUG
243 pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
244 mpu_addr, bank[1], addr[1], len,
245 wr_pr_debug_begin(data, len, read),
246 wr_pr_debug_end(read));
254 * inv_mpu_probe() - probe function.
256 static int inv_mpu_probe(struct i2c_client *client,
257 const struct i2c_device_id *id)
259 struct inv_mpu_state *st;
260 struct iio_dev *indio_dev;
263 #ifdef CONFIG_DTS_INV_MPU_IIO
264 enable_irq_wake(client->irq);
267 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
269 pr_err("I2c function error\n");
272 indio_dev = iio_allocate_device(sizeof(*st));
273 if (indio_dev == NULL) {
274 pr_err("memory allocation failed\n");
278 st = iio_priv(indio_dev);
280 st->sl_handle = client->adapter;
281 st->i2c_addr = client->addr;
282 #ifdef CONFIG_DTS_INV_MPU_IIO
283 result = invensense_mpu_parse_dt(&client->dev, &st->plat_data);
288 if (st->plat_data.power_on) {
289 result = st->plat_data.power_on(&st->plat_data);
291 dev_err(&client->dev,
292 "power_on failed: %d\n", result);
295 pr_info("%s: power on here.\n", __func__);
297 pr_info("%s: power on.\n", __func__);
302 *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
304 /* power is turned on inside check chip type*/
305 result = inv_check_chip_type(indio_dev, id->name);
309 /* Make state variables available to all _show and _store functions. */
310 i2c_set_clientdata(client, indio_dev);
311 indio_dev->dev.parent = &client->dev;
312 st->dev = &client->dev;
313 indio_dev->name = id->name;
314 st->irq = client->irq;
316 result = inv_mpu_configure_ring(indio_dev);
318 pr_err("configure ring buffer fail\n");
321 result = iio_buffer_register(indio_dev, indio_dev->channels,
322 indio_dev->num_channels);
324 pr_err("ring buffer register fail\n");
327 result = inv_mpu_probe_trigger(indio_dev);
329 pr_err("trigger probe fail\n");
330 goto out_remove_ring;
333 /* Tell the i2c counter, we have an IRQ */
334 INV_I2C_SETIRQ(IRQ_MPU, client->irq);
336 result = iio_device_register(indio_dev);
338 pr_err("IIO device register fail\n");
339 goto out_remove_trigger;
342 result = inv_create_dmp_sysfs(indio_dev);
344 pr_err("create dmp sysfs failed\n");
348 mutex_init(&st->suspend_resume_lock);
349 inv_init_sensor_struct(st);
350 dev_info(&client->dev, "%s is ready to go!\n", indio_dev->name);
354 iio_device_unregister(indio_dev);
356 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
357 inv_mpu_remove_trigger(indio_dev);
359 iio_buffer_unregister(indio_dev);
361 inv_mpu_unconfigure_ring(indio_dev);
363 iio_free_device(indio_dev);
365 dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
370 static void inv_mpu_shutdown(struct i2c_client *client)
372 struct iio_dev *indio_dev = i2c_get_clientdata(client);
373 struct inv_mpu_state *st = iio_priv(indio_dev);
376 mutex_lock(&indio_dev->mlock);
377 dev_dbg(&client->adapter->dev, "Shutting down %s...\n", st->hw->name);
379 /* reset to make sure previous state are not there */
380 result = inv_plat_single_write(st, REG_PWR_MGMT_1, BIT_H_RESET);
382 dev_err(&client->adapter->dev, "Failed to reset %s\n",
384 msleep(POWER_UP_TIME);
385 /* turn off power to ensure gyro engine is off */
386 result = inv_set_power(st, false);
388 dev_err(&client->adapter->dev, "Failed to turn off %s\n",
390 mutex_unlock(&indio_dev->mlock);
394 * inv_mpu_remove() - remove function.
396 static int inv_mpu_remove(struct i2c_client *client)
398 struct iio_dev *indio_dev = i2c_get_clientdata(client);
400 iio_device_unregister(indio_dev);
401 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
402 inv_mpu_remove_trigger(indio_dev);
403 iio_buffer_unregister(indio_dev);
404 inv_mpu_unconfigure_ring(indio_dev);
405 iio_free_device(indio_dev);
407 dev_info(&client->adapter->dev, "inv-mpu-iio module removed.\n");
415 * inv_mpu_resume(): resume method for this driver.
416 * This method can be modified according to the request of different
417 * customers. It basically undo everything suspend_noirq is doing
418 * and recover the chip to what it was before suspend.
420 static int inv_mpu_resume(struct device *dev)
422 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
423 struct inv_mpu_state *st = iio_priv(indio_dev);
425 /* add code according to different request Start */
426 pr_debug("%s inv_mpu_resume\n", st->hw->name);
427 mutex_lock(&indio_dev->mlock);
429 if (st->chip_config.dmp_on) {
431 inv_switch_power_in_lp(st, true);
432 write_be32_to_mem(st, st->batch.counter, BM_BATCH_THLD);
433 inv_plat_single_write(st, REG_INT_ENABLE_2,
434 BIT_FIFO_OVERFLOW_EN_0);
435 inv_switch_power_in_lp(st, false);
438 inv_switch_power_in_lp(st, true);
440 st->suspend_state = false;
441 mutex_unlock(&indio_dev->mlock);
442 inv_read_fifo(0, (void *)st);
443 /* add code according to different request End */
449 * inv_mpu_suspend(): suspend method for this driver.
450 * This method can be modified according to the request of different
451 * customers. If customer want some events, such as SMD to wake up the CPU,
452 * then data interrupt should be disabled in this interrupt to avoid
453 * unnecessary interrupts. If customer want pedometer running while CPU is
454 * asleep, then pedometer should be turned on while pedometer interrupt
455 * should be turned off.
457 static int inv_mpu_suspend(struct device *dev)
459 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
460 struct inv_mpu_state *st = iio_priv(indio_dev);
462 /* add code according to different request Start */
463 pr_debug("%s inv_mpu_suspend\n", st->hw->name);
464 mutex_lock(&indio_dev->mlock);
465 st->suspend_state = true;
467 if (st->chip_config.dmp_on) {
469 inv_switch_power_in_lp(st, true);
470 write_be32_to_mem(st, INT_MAX, BM_BATCH_THLD);
471 inv_plat_single_write(st, REG_INT_ENABLE_2, 0);
472 inv_switch_power_in_lp(st, false);
475 /* in non DMP case, just turn off the power */
476 inv_set_power(st, false);
478 mutex_unlock(&indio_dev->mlock);
480 /* add code according to different request End */
485 static const struct dev_pm_ops inv_mpu_pmops = {
486 .suspend = inv_mpu_suspend,
487 .resume = inv_mpu_resume,
489 #define INV_MPU_PMOPS (&inv_mpu_pmops)
491 #define INV_MPU_PMOPS NULL
492 #endif /* CONFIG_PM */
494 static const u16 normal_i2c[] = { I2C_CLIENT_END };
495 /* device id table is used to identify what device can be
496 * supported by this driver
498 static const struct i2c_device_id inv_mpu_id[] = {
499 {"mpu7400", ICM20628},
500 {"icm20728", ICM20728},
501 {"icm20628", ICM20628},
502 {"icm20645", ICM20628},
503 {"icm20630", ICM20628},
507 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
509 static struct i2c_driver inv_mpu_driver = {
510 .class = I2C_CLASS_HWMON,
511 .probe = inv_mpu_probe,
512 .remove = inv_mpu_remove,
513 .shutdown = inv_mpu_shutdown,
514 .id_table = inv_mpu_id,
516 .owner = THIS_MODULE,
517 .name = "inv-mpu-iio",
520 .address_list = normal_i2c,
523 static int __init inv_mpu_init(void)
525 int result = i2c_add_driver(&inv_mpu_driver);
533 static void __exit inv_mpu_exit(void)
535 i2c_del_driver(&inv_mpu_driver);
538 module_init(inv_mpu_init);
539 module_exit(inv_mpu_exit);
541 MODULE_AUTHOR("Invensense Corporation");
542 MODULE_DESCRIPTION("Invensense device driver");
543 MODULE_LICENSE("GPL");
544 MODULE_ALIAS("inv-mpu-iio");