1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Invensense, Inc. 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/delay.h> 8 #include <linux/err.h> 9 #include <linux/i2c.h> 10 #include <linux/iio/iio.h> 11 #include <linux/module.h> 12 #include <linux/of_device.h> 13 #include "inv_mpu_iio.h" 14 15 static const struct regmap_config inv_mpu_regmap_config = { 16 .reg_bits = 8, 17 .val_bits = 8, 18 }; 19 20 static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id) 21 { 22 struct iio_dev *indio_dev = i2c_mux_priv(muxc); 23 struct inv_mpu6050_state *st = iio_priv(indio_dev); 24 int ret; 25 26 mutex_lock(&st->lock); 27 28 ret = inv_mpu6050_set_power_itg(st, true); 29 if (ret) 30 goto error_unlock; 31 32 ret = regmap_write(st->map, st->reg->int_pin_cfg, 33 st->irq_mask | INV_MPU6050_BIT_BYPASS_EN); 34 35 error_unlock: 36 mutex_unlock(&st->lock); 37 38 return ret; 39 } 40 41 static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id) 42 { 43 struct iio_dev *indio_dev = i2c_mux_priv(muxc); 44 struct inv_mpu6050_state *st = iio_priv(indio_dev); 45 46 mutex_lock(&st->lock); 47 48 /* It doesn't really matter if any of the calls fail */ 49 regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask); 50 inv_mpu6050_set_power_itg(st, false); 51 52 mutex_unlock(&st->lock); 53 54 return 0; 55 } 56 57 static const char *inv_mpu_match_acpi_device(struct device *dev, 58 enum inv_devices *chip_id) 59 { 60 const struct acpi_device_id *id; 61 62 id = acpi_match_device(dev->driver->acpi_match_table, dev); 63 if (!id) 64 return NULL; 65 66 *chip_id = (int)id->driver_data; 67 68 return dev_name(dev); 69 } 70 71 static bool inv_mpu_i2c_aux_bus(struct device *dev) 72 { 73 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev)); 74 75 switch (st->chip_type) { 76 case INV_ICM20608: 77 case INV_ICM20602: 78 /* no i2c auxiliary bus on the chip */ 79 return false; 80 case INV_MPU9250: 81 case INV_MPU9255: 82 if (st->magn_disabled) 83 return true; 84 else 85 return false; 86 default: 87 return true; 88 } 89 } 90 91 /* 92 * MPU9xxx magnetometer support requires to disable i2c auxiliary bus support. 93 * To ensure backward compatibility with existing setups, do not disable 94 * i2c auxiliary bus if it used. 95 * Check for i2c-gate node in devicetree and set magnetometer disabled. 96 * Only MPU6500 is supported by ACPI, no need to check. 97 */ 98 static int inv_mpu_magn_disable(struct iio_dev *indio_dev) 99 { 100 struct inv_mpu6050_state *st = iio_priv(indio_dev); 101 struct device *dev = indio_dev->dev.parent; 102 struct device_node *mux_node; 103 104 switch (st->chip_type) { 105 case INV_MPU9250: 106 case INV_MPU9255: 107 mux_node = of_get_child_by_name(dev->of_node, "i2c-gate"); 108 if (mux_node != NULL) { 109 st->magn_disabled = true; 110 dev_warn(dev, "disable internal use of magnetometer\n"); 111 } 112 of_node_put(mux_node); 113 break; 114 default: 115 break; 116 } 117 118 return 0; 119 } 120 121 /** 122 * inv_mpu_probe() - probe function. 123 * @client: i2c client. 124 * @id: i2c device id. 125 * 126 * Returns 0 on success, a negative error code otherwise. 127 */ 128 static int inv_mpu_probe(struct i2c_client *client, 129 const struct i2c_device_id *id) 130 { 131 struct inv_mpu6050_state *st; 132 int result; 133 enum inv_devices chip_type; 134 struct regmap *regmap; 135 const char *name; 136 137 if (!i2c_check_functionality(client->adapter, 138 I2C_FUNC_SMBUS_I2C_BLOCK)) 139 return -EOPNOTSUPP; 140 141 if (client->dev.of_node) { 142 chip_type = (enum inv_devices) 143 of_device_get_match_data(&client->dev); 144 name = client->name; 145 } else if (id) { 146 chip_type = (enum inv_devices) 147 id->driver_data; 148 name = id->name; 149 } else if (ACPI_HANDLE(&client->dev)) { 150 name = inv_mpu_match_acpi_device(&client->dev, &chip_type); 151 if (!name) 152 return -ENODEV; 153 } else { 154 return -ENOSYS; 155 } 156 157 regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config); 158 if (IS_ERR(regmap)) { 159 dev_err(&client->dev, "Failed to register i2c regmap %d\n", 160 (int)PTR_ERR(regmap)); 161 return PTR_ERR(regmap); 162 } 163 164 result = inv_mpu_core_probe(regmap, client->irq, name, 165 inv_mpu_magn_disable, chip_type); 166 if (result < 0) 167 return result; 168 169 st = iio_priv(dev_get_drvdata(&client->dev)); 170 if (inv_mpu_i2c_aux_bus(&client->dev)) { 171 /* declare i2c auxiliary bus */ 172 st->muxc = i2c_mux_alloc(client->adapter, &client->dev, 173 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE, 174 inv_mpu6050_select_bypass, 175 inv_mpu6050_deselect_bypass); 176 if (!st->muxc) 177 return -ENOMEM; 178 st->muxc->priv = dev_get_drvdata(&client->dev); 179 result = i2c_mux_add_adapter(st->muxc, 0, 0, 0); 180 if (result) 181 return result; 182 result = inv_mpu_acpi_create_mux_client(client); 183 if (result) 184 goto out_del_mux; 185 } 186 187 return 0; 188 189 out_del_mux: 190 i2c_mux_del_adapters(st->muxc); 191 return result; 192 } 193 194 static int inv_mpu_remove(struct i2c_client *client) 195 { 196 struct iio_dev *indio_dev = i2c_get_clientdata(client); 197 struct inv_mpu6050_state *st = iio_priv(indio_dev); 198 199 if (st->muxc) { 200 inv_mpu_acpi_delete_mux_client(client); 201 i2c_mux_del_adapters(st->muxc); 202 } 203 204 return 0; 205 } 206 207 /* 208 * device id table is used to identify what device can be 209 * supported by this driver 210 */ 211 static const struct i2c_device_id inv_mpu_id[] = { 212 {"mpu6050", INV_MPU6050}, 213 {"mpu6500", INV_MPU6500}, 214 {"mpu6515", INV_MPU6515}, 215 {"mpu9150", INV_MPU9150}, 216 {"mpu9250", INV_MPU9250}, 217 {"mpu9255", INV_MPU9255}, 218 {"icm20608", INV_ICM20608}, 219 {"icm20602", INV_ICM20602}, 220 {} 221 }; 222 223 MODULE_DEVICE_TABLE(i2c, inv_mpu_id); 224 225 static const struct of_device_id inv_of_match[] = { 226 { 227 .compatible = "invensense,mpu6050", 228 .data = (void *)INV_MPU6050 229 }, 230 { 231 .compatible = "invensense,mpu6500", 232 .data = (void *)INV_MPU6500 233 }, 234 { 235 .compatible = "invensense,mpu6515", 236 .data = (void *)INV_MPU6515 237 }, 238 { 239 .compatible = "invensense,mpu9150", 240 .data = (void *)INV_MPU9150 241 }, 242 { 243 .compatible = "invensense,mpu9250", 244 .data = (void *)INV_MPU9250 245 }, 246 { 247 .compatible = "invensense,mpu9255", 248 .data = (void *)INV_MPU9255 249 }, 250 { 251 .compatible = "invensense,icm20608", 252 .data = (void *)INV_ICM20608 253 }, 254 { 255 .compatible = "invensense,icm20602", 256 .data = (void *)INV_ICM20602 257 }, 258 { } 259 }; 260 MODULE_DEVICE_TABLE(of, inv_of_match); 261 262 static const struct acpi_device_id inv_acpi_match[] = { 263 {"INVN6500", INV_MPU6500}, 264 { }, 265 }; 266 267 MODULE_DEVICE_TABLE(acpi, inv_acpi_match); 268 269 static struct i2c_driver inv_mpu_driver = { 270 .probe = inv_mpu_probe, 271 .remove = inv_mpu_remove, 272 .id_table = inv_mpu_id, 273 .driver = { 274 .of_match_table = inv_of_match, 275 .acpi_match_table = ACPI_PTR(inv_acpi_match), 276 .name = "inv-mpu6050-i2c", 277 .pm = &inv_mpu_pmops, 278 }, 279 }; 280 281 module_i2c_driver(inv_mpu_driver); 282 283 MODULE_AUTHOR("Invensense Corporation"); 284 MODULE_DESCRIPTION("Invensense device MPU6050 driver"); 285 MODULE_LICENSE("GPL"); 286