mpu3050-i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <linux/err.h>
  2. #include <linux/i2c.h>
  3. #include <linux/i2c-mux.h>
  4. #include <linux/iio/iio.h>
  5. #include <linux/module.h>
  6. #include <linux/regmap.h>
  7. #include <linux/pm_runtime.h>
  8. #include "mpu3050.h"
  9. static const struct regmap_config mpu3050_i2c_regmap_config = {
  10. .reg_bits = 8,
  11. .val_bits = 8,
  12. };
  13. static int mpu3050_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
  14. {
  15. struct mpu3050 *mpu3050 = i2c_mux_priv(mux);
  16. /* Just power up the device, that is all that is needed */
  17. pm_runtime_get_sync(mpu3050->dev);
  18. return 0;
  19. }
  20. static int mpu3050_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
  21. {
  22. struct mpu3050 *mpu3050 = i2c_mux_priv(mux);
  23. pm_runtime_mark_last_busy(mpu3050->dev);
  24. pm_runtime_put_autosuspend(mpu3050->dev);
  25. return 0;
  26. }
  27. static int mpu3050_i2c_probe(struct i2c_client *client,
  28. const struct i2c_device_id *id)
  29. {
  30. struct regmap *regmap;
  31. const char *name;
  32. struct mpu3050 *mpu3050;
  33. int ret;
  34. if (!i2c_check_functionality(client->adapter,
  35. I2C_FUNC_SMBUS_I2C_BLOCK))
  36. return -EOPNOTSUPP;
  37. if (id)
  38. name = id->name;
  39. else
  40. return -ENODEV;
  41. regmap = devm_regmap_init_i2c(client, &mpu3050_i2c_regmap_config);
  42. if (IS_ERR(regmap)) {
  43. dev_err(&client->dev, "Failed to register i2c regmap %d\n",
  44. (int)PTR_ERR(regmap));
  45. return PTR_ERR(regmap);
  46. }
  47. ret = mpu3050_common_probe(&client->dev, regmap, client->irq, name);
  48. if (ret)
  49. return ret;
  50. /* The main driver is up, now register the I2C mux */
  51. mpu3050 = iio_priv(dev_get_drvdata(&client->dev));
  52. mpu3050->i2cmux = i2c_mux_alloc(client->adapter, &client->dev,
  53. 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
  54. mpu3050_i2c_bypass_select,
  55. mpu3050_i2c_bypass_deselect);
  56. /* Just fail the mux, there is no point in killing the driver */
  57. if (!mpu3050->i2cmux)
  58. dev_err(&client->dev, "failed to allocate I2C mux\n");
  59. else {
  60. mpu3050->i2cmux->priv = mpu3050;
  61. /* Ignore failure, not critical */
  62. i2c_mux_add_adapter(mpu3050->i2cmux, 0, 0, 0);
  63. }
  64. return 0;
  65. }
  66. static int mpu3050_i2c_remove(struct i2c_client *client)
  67. {
  68. struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
  69. struct mpu3050 *mpu3050 = iio_priv(indio_dev);
  70. if (mpu3050->i2cmux)
  71. i2c_mux_del_adapters(mpu3050->i2cmux);
  72. return mpu3050_common_remove(&client->dev);
  73. }
  74. /*
  75. * device id table is used to identify what device can be
  76. * supported by this driver
  77. */
  78. static const struct i2c_device_id mpu3050_i2c_id[] = {
  79. { "mpu3050" },
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(i2c, mpu3050_i2c_id);
  83. static const struct of_device_id mpu3050_i2c_of_match[] = {
  84. { .compatible = "invensense,mpu3050", .data = "mpu3050" },
  85. /* Deprecated vendor ID from the Input driver */
  86. { .compatible = "invn,mpu3050", .data = "mpu3050" },
  87. { },
  88. };
  89. MODULE_DEVICE_TABLE(of, mpu3050_i2c_of_match);
  90. static struct i2c_driver mpu3050_i2c_driver = {
  91. .probe = mpu3050_i2c_probe,
  92. .remove = mpu3050_i2c_remove,
  93. .id_table = mpu3050_i2c_id,
  94. .driver = {
  95. .of_match_table = mpu3050_i2c_of_match,
  96. .name = "mpu3050-i2c",
  97. .pm = &mpu3050_dev_pm_ops,
  98. },
  99. };
  100. module_i2c_driver(mpu3050_i2c_driver);
  101. MODULE_AUTHOR("Linus Walleij");
  102. MODULE_DESCRIPTION("Invensense MPU3050 gyroscope driver");
  103. MODULE_LICENSE("GPL");