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/iio/iio.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include "inv_mpu_iio.h"
23 static const struct regmap_config inv_mpu_regmap_config
= {
28 static int inv_mpu6050_select_bypass(struct i2c_mux_core
*muxc
, u32 chan_id
)
30 struct iio_dev
*indio_dev
= i2c_mux_priv(muxc
);
31 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
34 /* Use the same mutex which was used everywhere to protect power-op */
35 mutex_lock(&st
->lock
);
36 if (!st
->powerup_count
) {
37 ret
= regmap_write(st
->map
, st
->reg
->pwr_mgmt_1
, 0);
41 usleep_range(INV_MPU6050_REG_UP_TIME_MIN
,
42 INV_MPU6050_REG_UP_TIME_MAX
);
46 ret
= regmap_write(st
->map
, st
->reg
->int_pin_cfg
,
47 INV_MPU6050_INT_PIN_CFG
|
48 INV_MPU6050_BIT_BYPASS_EN
);
51 mutex_unlock(&st
->lock
);
56 static int inv_mpu6050_deselect_bypass(struct i2c_mux_core
*muxc
, u32 chan_id
)
58 struct iio_dev
*indio_dev
= i2c_mux_priv(muxc
);
59 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
61 mutex_lock(&st
->lock
);
62 /* It doesn't really mattter, if any of the calls fails */
63 regmap_write(st
->map
, st
->reg
->int_pin_cfg
, INV_MPU6050_INT_PIN_CFG
);
65 if (!st
->powerup_count
)
66 regmap_write(st
->map
, st
->reg
->pwr_mgmt_1
,
67 INV_MPU6050_BIT_SLEEP
);
68 mutex_unlock(&st
->lock
);
73 static const char *inv_mpu_match_acpi_device(struct device
*dev
,
74 enum inv_devices
*chip_id
)
76 const struct acpi_device_id
*id
;
78 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
82 *chip_id
= (int)id
->driver_data
;
88 * inv_mpu_probe() - probe function.
89 * @client: i2c client.
92 * Returns 0 on success, a negative error code otherwise.
94 static int inv_mpu_probe(struct i2c_client
*client
,
95 const struct i2c_device_id
*id
)
97 struct inv_mpu6050_state
*st
;
99 enum inv_devices chip_type
;
100 struct regmap
*regmap
;
103 if (!i2c_check_functionality(client
->adapter
,
104 I2C_FUNC_SMBUS_I2C_BLOCK
))
107 if (client
->dev
.of_node
) {
108 chip_type
= (enum inv_devices
)
109 of_device_get_match_data(&client
->dev
);
112 chip_type
= (enum inv_devices
)
115 } else if (ACPI_HANDLE(&client
->dev
)) {
116 name
= inv_mpu_match_acpi_device(&client
->dev
, &chip_type
);
123 regmap
= devm_regmap_init_i2c(client
, &inv_mpu_regmap_config
);
124 if (IS_ERR(regmap
)) {
125 dev_err(&client
->dev
, "Failed to register i2c regmap %d\n",
126 (int)PTR_ERR(regmap
));
127 return PTR_ERR(regmap
);
130 result
= inv_mpu_core_probe(regmap
, client
->irq
, name
,
135 st
= iio_priv(dev_get_drvdata(&client
->dev
));
136 st
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
,
137 1, 0, I2C_MUX_LOCKED
| I2C_MUX_GATE
,
138 inv_mpu6050_select_bypass
,
139 inv_mpu6050_deselect_bypass
);
142 goto out_unreg_device
;
144 st
->muxc
->priv
= dev_get_drvdata(&client
->dev
);
145 result
= i2c_mux_add_adapter(st
->muxc
, 0, 0, 0);
147 goto out_unreg_device
;
149 result
= inv_mpu_acpi_create_mux_client(client
);
156 i2c_mux_del_adapters(st
->muxc
);
158 inv_mpu_core_remove(&client
->dev
);
162 static int inv_mpu_remove(struct i2c_client
*client
)
164 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
165 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
167 inv_mpu_acpi_delete_mux_client(client
);
168 i2c_mux_del_adapters(st
->muxc
);
170 return inv_mpu_core_remove(&client
->dev
);
174 * device id table is used to identify what device can be
175 * supported by this driver
177 static const struct i2c_device_id inv_mpu_id
[] = {
178 {"mpu6050", INV_MPU6050
},
179 {"mpu6500", INV_MPU6500
},
180 {"mpu9150", INV_MPU9150
},
181 {"mpu9250", INV_MPU9250
},
182 {"icm20608", INV_ICM20608
},
186 MODULE_DEVICE_TABLE(i2c
, inv_mpu_id
);
188 static const struct of_device_id inv_of_match
[] = {
190 .compatible
= "invensense,mpu6050",
191 .data
= (void *)INV_MPU6050
194 .compatible
= "invensense,mpu6500",
195 .data
= (void *)INV_MPU6500
198 .compatible
= "invensense,mpu9150",
199 .data
= (void *)INV_MPU9150
202 .compatible
= "invensense,mpu9250",
203 .data
= (void *)INV_MPU9250
206 .compatible
= "invensense,icm20608",
207 .data
= (void *)INV_ICM20608
211 MODULE_DEVICE_TABLE(of
, inv_of_match
);
213 static const struct acpi_device_id inv_acpi_match
[] = {
214 {"INVN6500", INV_MPU6500
},
218 MODULE_DEVICE_TABLE(acpi
, inv_acpi_match
);
220 static struct i2c_driver inv_mpu_driver
= {
221 .probe
= inv_mpu_probe
,
222 .remove
= inv_mpu_remove
,
223 .id_table
= inv_mpu_id
,
225 .of_match_table
= inv_of_match
,
226 .acpi_match_table
= ACPI_PTR(inv_acpi_match
),
227 .name
= "inv-mpu6050-i2c",
228 .pm
= &inv_mpu_pmops
,
232 module_i2c_driver(inv_mpu_driver
);
234 MODULE_AUTHOR("Invensense Corporation");
235 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
236 MODULE_LICENSE("GPL");