inv_mpu_i2c.c 5.0 KB

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