inv_mpu_i2c.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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;
  30. mutex_lock(&st->lock);
  31. ret = inv_mpu6050_set_power_itg(st, true);
  32. if (ret)
  33. goto error_unlock;
  34. ret = regmap_write(st->map, st->reg->int_pin_cfg,
  35. st->irq_mask | INV_MPU6050_BIT_BYPASS_EN);
  36. error_unlock:
  37. mutex_unlock(&st->lock);
  38. return ret;
  39. }
  40. static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id)
  41. {
  42. struct iio_dev *indio_dev = i2c_mux_priv(muxc);
  43. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  44. mutex_lock(&st->lock);
  45. /* It doesn't really matter if any of the calls fail */
  46. regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
  47. inv_mpu6050_set_power_itg(st, false);
  48. mutex_unlock(&st->lock);
  49. return 0;
  50. }
  51. static const char *inv_mpu_match_acpi_device(struct device *dev,
  52. enum inv_devices *chip_id)
  53. {
  54. const struct acpi_device_id *id;
  55. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  56. if (!id)
  57. return NULL;
  58. *chip_id = (int)id->driver_data;
  59. return dev_name(dev);
  60. }
  61. /**
  62. * inv_mpu_probe() - probe function.
  63. * @client: i2c client.
  64. * @id: i2c device id.
  65. *
  66. * Returns 0 on success, a negative error code otherwise.
  67. */
  68. static int inv_mpu_probe(struct i2c_client *client,
  69. const struct i2c_device_id *id)
  70. {
  71. struct inv_mpu6050_state *st;
  72. int result;
  73. enum inv_devices chip_type;
  74. struct regmap *regmap;
  75. const char *name;
  76. if (!i2c_check_functionality(client->adapter,
  77. I2C_FUNC_SMBUS_I2C_BLOCK))
  78. return -EOPNOTSUPP;
  79. if (client->dev.of_node) {
  80. chip_type = (enum inv_devices)
  81. of_device_get_match_data(&client->dev);
  82. name = client->name;
  83. } else if (id) {
  84. chip_type = (enum inv_devices)
  85. id->driver_data;
  86. name = id->name;
  87. } else if (ACPI_HANDLE(&client->dev)) {
  88. name = inv_mpu_match_acpi_device(&client->dev, &chip_type);
  89. if (!name)
  90. return -ENODEV;
  91. } else {
  92. return -ENOSYS;
  93. }
  94. regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
  95. if (IS_ERR(regmap)) {
  96. dev_err(&client->dev, "Failed to register i2c regmap %d\n",
  97. (int)PTR_ERR(regmap));
  98. return PTR_ERR(regmap);
  99. }
  100. result = inv_mpu_core_probe(regmap, client->irq, name,
  101. NULL, chip_type);
  102. if (result < 0)
  103. return result;
  104. st = iio_priv(dev_get_drvdata(&client->dev));
  105. switch (st->chip_type) {
  106. case INV_ICM20608:
  107. /* no i2c auxiliary bus on the chip */
  108. break;
  109. default:
  110. /* declare i2c auxiliary bus */
  111. st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
  112. 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
  113. inv_mpu6050_select_bypass,
  114. inv_mpu6050_deselect_bypass);
  115. if (!st->muxc)
  116. return -ENOMEM;
  117. st->muxc->priv = dev_get_drvdata(&client->dev);
  118. result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
  119. if (result)
  120. return result;
  121. result = inv_mpu_acpi_create_mux_client(client);
  122. if (result)
  123. goto out_del_mux;
  124. break;
  125. }
  126. return 0;
  127. out_del_mux:
  128. i2c_mux_del_adapters(st->muxc);
  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. if (st->muxc) {
  136. inv_mpu_acpi_delete_mux_client(client);
  137. i2c_mux_del_adapters(st->muxc);
  138. }
  139. return 0;
  140. }
  141. /*
  142. * device id table is used to identify what device can be
  143. * supported by this driver
  144. */
  145. static const struct i2c_device_id inv_mpu_id[] = {
  146. {"mpu6050", INV_MPU6050},
  147. {"mpu6500", INV_MPU6500},
  148. {"mpu6515", INV_MPU6515},
  149. {"mpu9150", INV_MPU9150},
  150. {"mpu9250", INV_MPU9250},
  151. {"mpu9255", INV_MPU9255},
  152. {"icm20608", INV_ICM20608},
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
  156. static const struct of_device_id inv_of_match[] = {
  157. {
  158. .compatible = "invensense,mpu6050",
  159. .data = (void *)INV_MPU6050
  160. },
  161. {
  162. .compatible = "invensense,mpu6500",
  163. .data = (void *)INV_MPU6500
  164. },
  165. {
  166. .compatible = "invensense,mpu6515",
  167. .data = (void *)INV_MPU6515
  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,mpu9255",
  179. .data = (void *)INV_MPU9255
  180. },
  181. {
  182. .compatible = "invensense,icm20608",
  183. .data = (void *)INV_ICM20608
  184. },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(of, inv_of_match);
  188. static const struct acpi_device_id inv_acpi_match[] = {
  189. {"INVN6500", INV_MPU6500},
  190. { },
  191. };
  192. MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
  193. static struct i2c_driver inv_mpu_driver = {
  194. .probe = inv_mpu_probe,
  195. .remove = inv_mpu_remove,
  196. .id_table = inv_mpu_id,
  197. .driver = {
  198. .of_match_table = inv_of_match,
  199. .acpi_match_table = ACPI_PTR(inv_acpi_match),
  200. .name = "inv-mpu6050-i2c",
  201. .pm = &inv_mpu_pmops,
  202. },
  203. };
  204. module_i2c_driver(inv_mpu_driver);
  205. MODULE_AUTHOR("Invensense Corporation");
  206. MODULE_DESCRIPTION("Invensense device MPU6050 driver");
  207. MODULE_LICENSE("GPL");