1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Invensense, Inc.
6 #include <linux/acpi.h>
7 #include <linux/delay.h>
10 #include <linux/iio/iio.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/property.h>
14 #include "inv_mpu_iio.h"
16 static const struct regmap_config inv_mpu_regmap_config
= {
21 static int inv_mpu6050_select_bypass(struct i2c_mux_core
*muxc
, u32 chan_id
)
26 static bool inv_mpu_i2c_aux_bus(struct device
*dev
)
28 struct inv_mpu6050_state
*st
= iio_priv(dev_get_drvdata(dev
));
30 switch (st
->chip_type
) {
36 /* no i2c auxiliary bus on the chip */
41 if (st
->magn_disabled
)
50 static int inv_mpu_i2c_aux_setup(struct iio_dev
*indio_dev
)
52 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
53 struct device
*dev
= indio_dev
->dev
.parent
;
54 struct device_node
*mux_node
;
58 * MPU9xxx magnetometer support requires to disable i2c auxiliary bus.
59 * To ensure backward compatibility with existing setups, do not disable
60 * i2c auxiliary bus if it used.
61 * Check for i2c-gate node in devicetree and set magnetometer disabled.
62 * Only MPU6500 is supported by ACPI, no need to check.
64 switch (st
->chip_type
) {
68 mux_node
= of_get_child_by_name(dev
->of_node
, "i2c-gate");
69 if (mux_node
!= NULL
) {
70 st
->magn_disabled
= true;
71 dev_warn(dev
, "disable internal use of magnetometer\n");
73 of_node_put(mux_node
);
79 /* enable i2c bypass when using i2c auxiliary bus */
80 if (inv_mpu_i2c_aux_bus(dev
)) {
81 ret
= regmap_write(st
->map
, st
->reg
->int_pin_cfg
,
82 st
->irq_mask
| INV_MPU6050_BIT_BYPASS_EN
);
91 * inv_mpu_probe() - probe function.
92 * @client: i2c client.
95 * Returns 0 on success, a negative error code otherwise.
97 static int inv_mpu_probe(struct i2c_client
*client
,
98 const struct i2c_device_id
*id
)
101 struct inv_mpu6050_state
*st
;
103 enum inv_devices chip_type
;
104 struct regmap
*regmap
;
107 if (!i2c_check_functionality(client
->adapter
,
108 I2C_FUNC_SMBUS_I2C_BLOCK
))
111 match
= device_get_match_data(&client
->dev
);
113 chip_type
= (enum inv_devices
)match
;
116 chip_type
= (enum inv_devices
)
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
,
131 inv_mpu_i2c_aux_setup
, chip_type
);
135 st
= iio_priv(dev_get_drvdata(&client
->dev
));
136 if (inv_mpu_i2c_aux_bus(&client
->dev
)) {
137 /* declare i2c auxiliary bus */
138 st
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
,
139 1, 0, I2C_MUX_LOCKED
| I2C_MUX_GATE
,
140 inv_mpu6050_select_bypass
, NULL
);
143 st
->muxc
->priv
= dev_get_drvdata(&client
->dev
);
144 result
= i2c_mux_add_adapter(st
->muxc
, 0, 0, 0);
147 result
= inv_mpu_acpi_create_mux_client(client
);
155 i2c_mux_del_adapters(st
->muxc
);
159 static int inv_mpu_remove(struct i2c_client
*client
)
161 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
162 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
165 inv_mpu_acpi_delete_mux_client(client
);
166 i2c_mux_del_adapters(st
->muxc
);
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
},
179 {"mpu6515", INV_MPU6515
},
180 {"mpu9150", INV_MPU9150
},
181 {"mpu9250", INV_MPU9250
},
182 {"mpu9255", INV_MPU9255
},
183 {"icm20608", INV_ICM20608
},
184 {"icm20609", INV_ICM20609
},
185 {"icm20689", INV_ICM20689
},
186 {"icm20602", INV_ICM20602
},
187 {"icm20690", INV_ICM20690
},
188 {"iam20680", INV_IAM20680
},
192 MODULE_DEVICE_TABLE(i2c
, inv_mpu_id
);
194 static const struct of_device_id inv_of_match
[] = {
196 .compatible
= "invensense,mpu6050",
197 .data
= (void *)INV_MPU6050
200 .compatible
= "invensense,mpu6500",
201 .data
= (void *)INV_MPU6500
204 .compatible
= "invensense,mpu6515",
205 .data
= (void *)INV_MPU6515
208 .compatible
= "invensense,mpu9150",
209 .data
= (void *)INV_MPU9150
212 .compatible
= "invensense,mpu9250",
213 .data
= (void *)INV_MPU9250
216 .compatible
= "invensense,mpu9255",
217 .data
= (void *)INV_MPU9255
220 .compatible
= "invensense,icm20608",
221 .data
= (void *)INV_ICM20608
224 .compatible
= "invensense,icm20609",
225 .data
= (void *)INV_ICM20609
228 .compatible
= "invensense,icm20689",
229 .data
= (void *)INV_ICM20689
232 .compatible
= "invensense,icm20602",
233 .data
= (void *)INV_ICM20602
236 .compatible
= "invensense,icm20690",
237 .data
= (void *)INV_ICM20690
240 .compatible
= "invensense,iam20680",
241 .data
= (void *)INV_IAM20680
245 MODULE_DEVICE_TABLE(of
, inv_of_match
);
247 static const struct acpi_device_id inv_acpi_match
[] = {
248 {"INVN6500", INV_MPU6500
},
252 MODULE_DEVICE_TABLE(acpi
, inv_acpi_match
);
254 static struct i2c_driver inv_mpu_driver
= {
255 .probe
= inv_mpu_probe
,
256 .remove
= inv_mpu_remove
,
257 .id_table
= inv_mpu_id
,
259 .of_match_table
= inv_of_match
,
260 .acpi_match_table
= ACPI_PTR(inv_acpi_match
),
261 .name
= "inv-mpu6050-i2c",
262 .pm
= &inv_mpu_pmops
,
266 module_i2c_driver(inv_mpu_driver
);
268 MODULE_AUTHOR("Invensense Corporation");
269 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
270 MODULE_LICENSE("GPL");