inv_mpu_i2c.c 5.7 KB

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