inv_mpu_i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. case INV_ICM20602:
  108. /* no i2c auxiliary bus on the chip */
  109. break;
  110. default:
  111. /* declare i2c auxiliary bus */
  112. st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
  113. 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
  114. inv_mpu6050_select_bypass,
  115. inv_mpu6050_deselect_bypass);
  116. if (!st->muxc)
  117. return -ENOMEM;
  118. st->muxc->priv = dev_get_drvdata(&client->dev);
  119. result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
  120. if (result)
  121. return result;
  122. result = inv_mpu_acpi_create_mux_client(client);
  123. if (result)
  124. goto out_del_mux;
  125. break;
  126. }
  127. return 0;
  128. out_del_mux:
  129. i2c_mux_del_adapters(st->muxc);
  130. return result;
  131. }
  132. static int inv_mpu_remove(struct i2c_client *client)
  133. {
  134. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  135. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  136. if (st->muxc) {
  137. inv_mpu_acpi_delete_mux_client(client);
  138. i2c_mux_del_adapters(st->muxc);
  139. }
  140. return 0;
  141. }
  142. /*
  143. * device id table is used to identify what device can be
  144. * supported by this driver
  145. */
  146. static const struct i2c_device_id inv_mpu_id[] = {
  147. {"mpu6050", INV_MPU6050},
  148. {"mpu6500", INV_MPU6500},
  149. {"mpu6515", INV_MPU6515},
  150. {"mpu9150", INV_MPU9150},
  151. {"mpu9250", INV_MPU9250},
  152. {"mpu9255", INV_MPU9255},
  153. {"icm20608", INV_ICM20608},
  154. {"icm20602", INV_ICM20602},
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
  158. static const struct of_device_id inv_of_match[] = {
  159. {
  160. .compatible = "invensense,mpu6050",
  161. .data = (void *)INV_MPU6050
  162. },
  163. {
  164. .compatible = "invensense,mpu6500",
  165. .data = (void *)INV_MPU6500
  166. },
  167. {
  168. .compatible = "invensense,mpu6515",
  169. .data = (void *)INV_MPU6515
  170. },
  171. {
  172. .compatible = "invensense,mpu9150",
  173. .data = (void *)INV_MPU9150
  174. },
  175. {
  176. .compatible = "invensense,mpu9250",
  177. .data = (void *)INV_MPU9250
  178. },
  179. {
  180. .compatible = "invensense,mpu9255",
  181. .data = (void *)INV_MPU9255
  182. },
  183. {
  184. .compatible = "invensense,icm20608",
  185. .data = (void *)INV_ICM20608
  186. },
  187. {
  188. .compatible = "invensense,icm20602",
  189. .data = (void *)INV_ICM20602
  190. },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(of, inv_of_match);
  194. static const struct acpi_device_id inv_acpi_match[] = {
  195. {"INVN6500", INV_MPU6500},
  196. { },
  197. };
  198. MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
  199. static struct i2c_driver inv_mpu_driver = {
  200. .probe = inv_mpu_probe,
  201. .remove = inv_mpu_remove,
  202. .id_table = inv_mpu_id,
  203. .driver = {
  204. .of_match_table = inv_of_match,
  205. .acpi_match_table = ACPI_PTR(inv_acpi_match),
  206. .name = "inv-mpu6050-i2c",
  207. .pm = &inv_mpu_pmops,
  208. },
  209. };
  210. module_i2c_driver(inv_mpu_driver);
  211. MODULE_AUTHOR("Invensense Corporation");
  212. MODULE_DESCRIPTION("Invensense device MPU6050 driver");
  213. MODULE_LICENSE("GPL");