Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/err.h>
0003 #include <linux/i2c.h>
0004 #include <linux/i2c-mux.h>
0005 #include <linux/iio/iio.h>
0006 #include <linux/module.h>
0007 #include <linux/regmap.h>
0008 #include <linux/pm_runtime.h>
0009 
0010 #include "mpu3050.h"
0011 
0012 static const struct regmap_config mpu3050_i2c_regmap_config = {
0013     .reg_bits = 8,
0014     .val_bits = 8,
0015 };
0016 
0017 static int mpu3050_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
0018 {
0019     struct mpu3050 *mpu3050 = i2c_mux_priv(mux);
0020 
0021     /* Just power up the device, that is all that is needed */
0022     pm_runtime_get_sync(mpu3050->dev);
0023     return 0;
0024 }
0025 
0026 static int mpu3050_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
0027 {
0028     struct mpu3050 *mpu3050 = i2c_mux_priv(mux);
0029 
0030     pm_runtime_mark_last_busy(mpu3050->dev);
0031     pm_runtime_put_autosuspend(mpu3050->dev);
0032     return 0;
0033 }
0034 
0035 static int mpu3050_i2c_probe(struct i2c_client *client,
0036                  const struct i2c_device_id *id)
0037 {
0038     struct regmap *regmap;
0039     const char *name;
0040     struct mpu3050 *mpu3050;
0041     int ret;
0042 
0043     if (!i2c_check_functionality(client->adapter,
0044                      I2C_FUNC_SMBUS_I2C_BLOCK))
0045         return -EOPNOTSUPP;
0046 
0047     if (id)
0048         name = id->name;
0049     else
0050         return -ENODEV;
0051 
0052     regmap = devm_regmap_init_i2c(client, &mpu3050_i2c_regmap_config);
0053     if (IS_ERR(regmap)) {
0054         dev_err(&client->dev, "Failed to register i2c regmap: %pe\n",
0055             regmap);
0056         return PTR_ERR(regmap);
0057     }
0058 
0059     ret = mpu3050_common_probe(&client->dev, regmap, client->irq, name);
0060     if (ret)
0061         return ret;
0062 
0063     /* The main driver is up, now register the I2C mux */
0064     mpu3050 = iio_priv(dev_get_drvdata(&client->dev));
0065     mpu3050->i2cmux = i2c_mux_alloc(client->adapter, &client->dev,
0066                     1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
0067                     mpu3050_i2c_bypass_select,
0068                     mpu3050_i2c_bypass_deselect);
0069     /* Just fail the mux, there is no point in killing the driver */
0070     if (!mpu3050->i2cmux)
0071         dev_err(&client->dev, "failed to allocate I2C mux\n");
0072     else {
0073         mpu3050->i2cmux->priv = mpu3050;
0074         /* Ignore failure, not critical */
0075         i2c_mux_add_adapter(mpu3050->i2cmux, 0, 0, 0);
0076     }
0077 
0078     return 0;
0079 }
0080 
0081 static int mpu3050_i2c_remove(struct i2c_client *client)
0082 {
0083     struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
0084     struct mpu3050 *mpu3050 = iio_priv(indio_dev);
0085 
0086     if (mpu3050->i2cmux)
0087         i2c_mux_del_adapters(mpu3050->i2cmux);
0088 
0089     mpu3050_common_remove(&client->dev);
0090 
0091     return 0;
0092 }
0093 
0094 /*
0095  * device id table is used to identify what device can be
0096  * supported by this driver
0097  */
0098 static const struct i2c_device_id mpu3050_i2c_id[] = {
0099     { "mpu3050" },
0100     {}
0101 };
0102 MODULE_DEVICE_TABLE(i2c, mpu3050_i2c_id);
0103 
0104 static const struct of_device_id mpu3050_i2c_of_match[] = {
0105     { .compatible = "invensense,mpu3050", .data = "mpu3050" },
0106     /* Deprecated vendor ID from the Input driver */
0107     { .compatible = "invn,mpu3050", .data = "mpu3050" },
0108     { },
0109 };
0110 MODULE_DEVICE_TABLE(of, mpu3050_i2c_of_match);
0111 
0112 static struct i2c_driver mpu3050_i2c_driver = {
0113     .probe = mpu3050_i2c_probe,
0114     .remove = mpu3050_i2c_remove,
0115     .id_table = mpu3050_i2c_id,
0116     .driver = {
0117         .of_match_table = mpu3050_i2c_of_match,
0118         .name = "mpu3050-i2c",
0119         .pm = pm_ptr(&mpu3050_dev_pm_ops),
0120     },
0121 };
0122 module_i2c_driver(mpu3050_i2c_driver);
0123 
0124 MODULE_AUTHOR("Linus Walleij");
0125 MODULE_DESCRIPTION("Invensense MPU3050 gyroscope driver");
0126 MODULE_LICENSE("GPL");