mpu3050-i2c.c 3.1 KB

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