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