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.
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/i2c-mux.h>
19 #include <linux/iio/iio.h>
20 #include <linux/module.h>
21 #include "inv_mpu_iio.h"
23 static const struct regmap_config inv_mpu_regmap_config = {
29 * The i2c read/write needs to happen in unlocked mode. As the parent
30 * adapter is common. If we use locked versions, it will fail as
31 * the mux adapter will lock the parent i2c adapter, while calling
32 * select/deselect functions.
34 static int inv_mpu6050_write_reg_unlocked(struct i2c_client *client,
39 struct i2c_msg msg[1] = {
48 ret = __i2c_transfer(client->adapter, msg, 1);
55 static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
58 struct i2c_client *client = mux_priv;
59 struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
60 struct inv_mpu6050_state *st = iio_priv(indio_dev);
63 /* Use the same mutex which was used everywhere to protect power-op */
64 mutex_lock(&indio_dev->mlock);
65 if (!st->powerup_count) {
66 ret = inv_mpu6050_write_reg_unlocked(client,
67 st->reg->pwr_mgmt_1, 0);
71 msleep(INV_MPU6050_REG_UP_TIME);
75 ret = inv_mpu6050_write_reg_unlocked(client,
77 INV_MPU6050_INT_PIN_CFG |
78 INV_MPU6050_BIT_BYPASS_EN);
81 mutex_unlock(&indio_dev->mlock);
86 static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
87 void *mux_priv, u32 chan_id)
89 struct i2c_client *client = mux_priv;
90 struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
91 struct inv_mpu6050_state *st = iio_priv(indio_dev);
93 mutex_lock(&indio_dev->mlock);
94 /* It doesn't really mattter, if any of the calls fails */
95 inv_mpu6050_write_reg_unlocked(client, st->reg->int_pin_cfg,
96 INV_MPU6050_INT_PIN_CFG);
98 if (!st->powerup_count)
99 inv_mpu6050_write_reg_unlocked(client, st->reg->pwr_mgmt_1,
100 INV_MPU6050_BIT_SLEEP);
101 mutex_unlock(&indio_dev->mlock);
107 * inv_mpu_probe() - probe function.
108 * @client: i2c client.
109 * @id: i2c device id.
111 * Returns 0 on success, a negative error code otherwise.
113 static int inv_mpu_probe(struct i2c_client *client,
114 const struct i2c_device_id *id)
116 struct inv_mpu6050_state *st;
118 const char *name = id ? id->name : NULL;
119 struct regmap *regmap;
121 if (!i2c_check_functionality(client->adapter,
122 I2C_FUNC_SMBUS_I2C_BLOCK))
125 regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
126 if (IS_ERR(regmap)) {
127 dev_err(&client->dev, "Failed to register i2c regmap %d\n",
128 (int)PTR_ERR(regmap));
129 return PTR_ERR(regmap);
132 result = inv_mpu_core_probe(regmap, client->irq, name, NULL);
136 st = iio_priv(dev_get_drvdata(&client->dev));
137 st->mux_adapter = i2c_add_mux_adapter(client->adapter,
141 inv_mpu6050_select_bypass,
142 inv_mpu6050_deselect_bypass);
143 if (!st->mux_adapter) {
145 goto out_unreg_device;
148 result = inv_mpu_acpi_create_mux_client(client);
155 i2c_del_mux_adapter(st->mux_adapter);
157 inv_mpu_core_remove(&client->dev);
161 static int inv_mpu_remove(struct i2c_client *client)
163 struct iio_dev *indio_dev = i2c_get_clientdata(client);
164 struct inv_mpu6050_state *st = iio_priv(indio_dev);
166 inv_mpu_acpi_delete_mux_client(client);
167 i2c_del_mux_adapter(st->mux_adapter);
169 return inv_mpu_core_remove(&client->dev);
173 * device id table is used to identify what device can be
174 * supported by this driver
176 static const struct i2c_device_id inv_mpu_id[] = {
177 {"mpu6050", INV_MPU6050},
178 {"mpu6500", INV_MPU6500},
182 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
184 static const struct acpi_device_id inv_acpi_match[] = {
189 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
191 static struct i2c_driver inv_mpu_driver = {
192 .probe = inv_mpu_probe,
193 .remove = inv_mpu_remove,
194 .id_table = inv_mpu_id,
196 .acpi_match_table = ACPI_PTR(inv_acpi_match),
197 .name = "inv-mpu6050-i2c",
198 .pm = &inv_mpu_pmops,
202 module_i2c_driver(inv_mpu_driver);
204 MODULE_AUTHOR("Invensense Corporation");
205 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
206 MODULE_LICENSE("GPL");