1 /* 2 * Copyright (C) 2012 Invensense, Inc. 3 * 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. 7 * 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. 12 */ 13 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 "inv_mpu_iio.h" 21 22 static const struct regmap_config inv_mpu_regmap_config = { 23 .reg_bits = 8, 24 .val_bits = 8, 25 }; 26 27 static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id) 28 { 29 struct iio_dev *indio_dev = i2c_mux_priv(muxc); 30 struct inv_mpu6050_state *st = iio_priv(indio_dev); 31 int ret = 0; 32 33 /* Use the same mutex which was used everywhere to protect power-op */ 34 mutex_lock(&indio_dev->mlock); 35 if (!st->powerup_count) { 36 ret = regmap_write(st->map, st->reg->pwr_mgmt_1, 0); 37 if (ret) 38 goto write_error; 39 40 usleep_range(INV_MPU6050_REG_UP_TIME_MIN, 41 INV_MPU6050_REG_UP_TIME_MAX); 42 } 43 if (!ret) { 44 st->powerup_count++; 45 ret = regmap_write(st->map, st->reg->int_pin_cfg, 46 INV_MPU6050_INT_PIN_CFG | 47 INV_MPU6050_BIT_BYPASS_EN); 48 } 49 write_error: 50 mutex_unlock(&indio_dev->mlock); 51 52 return ret; 53 } 54 55 static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id) 56 { 57 struct iio_dev *indio_dev = i2c_mux_priv(muxc); 58 struct inv_mpu6050_state *st = iio_priv(indio_dev); 59 60 mutex_lock(&indio_dev->mlock); 61 /* It doesn't really mattter, if any of the calls fails */ 62 regmap_write(st->map, st->reg->int_pin_cfg, INV_MPU6050_INT_PIN_CFG); 63 st->powerup_count--; 64 if (!st->powerup_count) 65 regmap_write(st->map, st->reg->pwr_mgmt_1, 66 INV_MPU6050_BIT_SLEEP); 67 mutex_unlock(&indio_dev->mlock); 68 69 return 0; 70 } 71 72 static const char *inv_mpu_match_acpi_device(struct device *dev, int *chip_id) 73 { 74 const struct acpi_device_id *id; 75 76 id = acpi_match_device(dev->driver->acpi_match_table, dev); 77 if (!id) 78 return NULL; 79 80 *chip_id = (int)id->driver_data; 81 82 return dev_name(dev); 83 } 84 85 /** 86 * inv_mpu_probe() - probe function. 87 * @client: i2c client. 88 * @id: i2c device id. 89 * 90 * Returns 0 on success, a negative error code otherwise. 91 */ 92 static int inv_mpu_probe(struct i2c_client *client, 93 const struct i2c_device_id *id) 94 { 95 struct inv_mpu6050_state *st; 96 int result, chip_type; 97 struct regmap *regmap; 98 const char *name; 99 100 if (!i2c_check_functionality(client->adapter, 101 I2C_FUNC_SMBUS_I2C_BLOCK)) 102 return -EOPNOTSUPP; 103 104 if (id) { 105 chip_type = (int)id->driver_data; 106 name = id->name; 107 } else if (ACPI_HANDLE(&client->dev)) { 108 name = inv_mpu_match_acpi_device(&client->dev, &chip_type); 109 if (!name) 110 return -ENODEV; 111 } else { 112 return -ENOSYS; 113 } 114 115 regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config); 116 if (IS_ERR(regmap)) { 117 dev_err(&client->dev, "Failed to register i2c regmap %d\n", 118 (int)PTR_ERR(regmap)); 119 return PTR_ERR(regmap); 120 } 121 122 result = inv_mpu_core_probe(regmap, client->irq, name, 123 NULL, chip_type); 124 if (result < 0) 125 return result; 126 127 st = iio_priv(dev_get_drvdata(&client->dev)); 128 st->muxc = i2c_mux_alloc(client->adapter, &client->dev, 129 1, 0, I2C_MUX_LOCKED, 130 inv_mpu6050_select_bypass, 131 inv_mpu6050_deselect_bypass); 132 if (!st->muxc) { 133 result = -ENOMEM; 134 goto out_unreg_device; 135 } 136 st->muxc->priv = dev_get_drvdata(&client->dev); 137 result = i2c_mux_add_adapter(st->muxc, 0, 0, 0); 138 if (result) 139 goto out_unreg_device; 140 141 result = inv_mpu_acpi_create_mux_client(client); 142 if (result) 143 goto out_del_mux; 144 145 return 0; 146 147 out_del_mux: 148 i2c_mux_del_adapters(st->muxc); 149 out_unreg_device: 150 inv_mpu_core_remove(&client->dev); 151 return result; 152 } 153 154 static int inv_mpu_remove(struct i2c_client *client) 155 { 156 struct iio_dev *indio_dev = i2c_get_clientdata(client); 157 struct inv_mpu6050_state *st = iio_priv(indio_dev); 158 159 inv_mpu_acpi_delete_mux_client(client); 160 i2c_mux_del_adapters(st->muxc); 161 162 return inv_mpu_core_remove(&client->dev); 163 } 164 165 /* 166 * device id table is used to identify what device can be 167 * supported by this driver 168 */ 169 static const struct i2c_device_id inv_mpu_id[] = { 170 {"mpu6050", INV_MPU6050}, 171 {"mpu6500", INV_MPU6500}, 172 {"mpu9150", INV_MPU9150}, 173 {"icm20608", INV_ICM20608}, 174 {} 175 }; 176 177 MODULE_DEVICE_TABLE(i2c, inv_mpu_id); 178 179 static const struct acpi_device_id inv_acpi_match[] = { 180 {"INVN6500", INV_MPU6500}, 181 { }, 182 }; 183 184 MODULE_DEVICE_TABLE(acpi, inv_acpi_match); 185 186 static struct i2c_driver inv_mpu_driver = { 187 .probe = inv_mpu_probe, 188 .remove = inv_mpu_remove, 189 .id_table = inv_mpu_id, 190 .driver = { 191 .acpi_match_table = ACPI_PTR(inv_acpi_match), 192 .name = "inv-mpu6050-i2c", 193 .pm = &inv_mpu_pmops, 194 }, 195 }; 196 197 module_i2c_driver(inv_mpu_driver); 198 199 MODULE_AUTHOR("Invensense Corporation"); 200 MODULE_DESCRIPTION("Invensense device MPU6050 driver"); 201 MODULE_LICENSE("GPL"); 202