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