2019-05-29 22:17:56 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-02-12 19:44:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Invensense, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/acpi.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/module.h>
|
2017-03-15 12:44:56 +08:00
|
|
|
#include <linux/of_device.h>
|
2020-02-06 18:31:00 +08:00
|
|
|
#include <linux/property.h>
|
2016-02-12 19:44:44 +08:00
|
|
|
#include "inv_mpu_iio.h"
|
|
|
|
|
|
|
|
static const struct regmap_config inv_mpu_regmap_config = {
|
|
|
|
.reg_bits = 8,
|
|
|
|
.val_bits = 8,
|
|
|
|
};
|
|
|
|
|
2016-04-20 14:40:49 +08:00
|
|
|
static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id)
|
2016-02-12 19:44:44 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:41:58 +08:00
|
|
|
static bool inv_mpu_i2c_aux_bus(struct device *dev)
|
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
|
|
|
|
|
|
|
|
switch (st->chip_type) {
|
|
|
|
case INV_ICM20608:
|
2020-02-06 18:31:01 +08:00
|
|
|
case INV_ICM20609:
|
|
|
|
case INV_ICM20689:
|
2019-09-16 17:41:58 +08:00
|
|
|
case INV_ICM20602:
|
2020-02-06 18:31:02 +08:00
|
|
|
case INV_IAM20680:
|
2019-09-16 17:41:58 +08:00
|
|
|
/* no i2c auxiliary bus on the chip */
|
|
|
|
return false;
|
2019-11-15 22:06:22 +08:00
|
|
|
case INV_MPU9150:
|
2019-09-16 17:41:58 +08:00
|
|
|
case INV_MPU9250:
|
|
|
|
case INV_MPU9255:
|
|
|
|
if (st->magn_disabled)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:39:46 +08:00
|
|
|
static int inv_mpu_i2c_aux_setup(struct iio_dev *indio_dev)
|
2019-09-16 17:41:58 +08:00
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
|
|
|
struct device *dev = indio_dev->dev.parent;
|
|
|
|
struct device_node *mux_node;
|
2020-02-19 22:39:46 +08:00
|
|
|
int ret;
|
2019-09-16 17:41:58 +08:00
|
|
|
|
2020-02-19 22:39:46 +08:00
|
|
|
/*
|
|
|
|
* MPU9xxx magnetometer support requires to disable i2c auxiliary bus.
|
|
|
|
* To ensure backward compatibility with existing setups, do not disable
|
|
|
|
* i2c auxiliary bus if it used.
|
|
|
|
* Check for i2c-gate node in devicetree and set magnetometer disabled.
|
|
|
|
* Only MPU6500 is supported by ACPI, no need to check.
|
|
|
|
*/
|
2019-09-16 17:41:58 +08:00
|
|
|
switch (st->chip_type) {
|
2019-11-15 22:06:22 +08:00
|
|
|
case INV_MPU9150:
|
2019-09-16 17:41:58 +08:00
|
|
|
case INV_MPU9250:
|
|
|
|
case INV_MPU9255:
|
|
|
|
mux_node = of_get_child_by_name(dev->of_node, "i2c-gate");
|
|
|
|
if (mux_node != NULL) {
|
|
|
|
st->magn_disabled = true;
|
|
|
|
dev_warn(dev, "disable internal use of magnetometer\n");
|
|
|
|
}
|
|
|
|
of_node_put(mux_node);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:39:46 +08:00
|
|
|
/* enable i2c bypass when using i2c auxiliary bus */
|
|
|
|
if (inv_mpu_i2c_aux_bus(dev)) {
|
|
|
|
ret = regmap_write(st->map, st->reg->int_pin_cfg,
|
|
|
|
st->irq_mask | INV_MPU6050_BIT_BYPASS_EN);
|
|
|
|
if (ret)
|
2020-02-19 22:39:48 +08:00
|
|
|
return ret;
|
2020-02-19 22:39:46 +08:00
|
|
|
}
|
|
|
|
|
2019-09-16 17:41:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:44:44 +08:00
|
|
|
/**
|
|
|
|
* inv_mpu_probe() - probe function.
|
|
|
|
* @client: i2c client.
|
|
|
|
* @id: i2c device id.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, a negative error code otherwise.
|
|
|
|
*/
|
|
|
|
static int inv_mpu_probe(struct i2c_client *client,
|
2016-02-18 23:53:12 +08:00
|
|
|
const struct i2c_device_id *id)
|
2016-02-12 19:44:44 +08:00
|
|
|
{
|
2020-02-06 18:31:00 +08:00
|
|
|
const void *match;
|
2016-02-12 19:44:44 +08:00
|
|
|
struct inv_mpu6050_state *st;
|
2017-03-15 12:44:56 +08:00
|
|
|
int result;
|
|
|
|
enum inv_devices chip_type;
|
2016-02-12 19:44:44 +08:00
|
|
|
struct regmap *regmap;
|
2016-03-18 00:32:44 +08:00
|
|
|
const char *name;
|
2016-02-12 19:44:44 +08:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter,
|
|
|
|
I2C_FUNC_SMBUS_I2C_BLOCK))
|
2016-02-27 14:13:49 +08:00
|
|
|
return -EOPNOTSUPP;
|
2016-02-12 19:44:44 +08:00
|
|
|
|
2020-02-06 18:31:00 +08:00
|
|
|
match = device_get_match_data(&client->dev);
|
|
|
|
if (match) {
|
|
|
|
chip_type = (enum inv_devices)match;
|
2017-03-15 12:44:56 +08:00
|
|
|
name = client->name;
|
|
|
|
} else if (id) {
|
|
|
|
chip_type = (enum inv_devices)
|
|
|
|
id->driver_data;
|
2016-03-18 00:32:44 +08:00
|
|
|
name = id->name;
|
|
|
|
} else {
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:44:44 +08:00
|
|
|
regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
|
|
|
|
if (IS_ERR(regmap)) {
|
2020-03-23 01:23:11 +08:00
|
|
|
dev_err(&client->dev, "Failed to register i2c regmap: %pe\n",
|
|
|
|
regmap);
|
2016-02-12 19:44:44 +08:00
|
|
|
return PTR_ERR(regmap);
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:39:11 +08:00
|
|
|
result = inv_mpu_core_probe(regmap, client->irq, name,
|
2020-02-19 22:39:46 +08:00
|
|
|
inv_mpu_i2c_aux_setup, chip_type);
|
2016-02-12 19:44:44 +08:00
|
|
|
if (result < 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
st = iio_priv(dev_get_drvdata(&client->dev));
|
2019-09-16 17:41:58 +08:00
|
|
|
if (inv_mpu_i2c_aux_bus(&client->dev)) {
|
2018-04-30 18:14:08 +08:00
|
|
|
/* declare i2c auxiliary bus */
|
|
|
|
st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
|
|
|
|
1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
|
2020-02-19 22:39:46 +08:00
|
|
|
inv_mpu6050_select_bypass, NULL);
|
2018-04-30 18:14:08 +08:00
|
|
|
if (!st->muxc)
|
|
|
|
return -ENOMEM;
|
|
|
|
st->muxc->priv = dev_get_drvdata(&client->dev);
|
|
|
|
result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
result = inv_mpu_acpi_create_mux_client(client);
|
|
|
|
if (result)
|
|
|
|
goto out_del_mux;
|
|
|
|
}
|
2016-02-12 19:44:44 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_del_mux:
|
2016-04-20 14:40:49 +08:00
|
|
|
i2c_mux_del_adapters(st->muxc);
|
2016-02-12 19:44:44 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inv_mpu_remove(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
|
|
|
|
2018-04-30 18:14:08 +08:00
|
|
|
if (st->muxc) {
|
|
|
|
inv_mpu_acpi_delete_mux_client(client);
|
|
|
|
i2c_mux_del_adapters(st->muxc);
|
|
|
|
}
|
2016-02-12 19:44:44 +08:00
|
|
|
|
2018-04-23 18:33:31 +08:00
|
|
|
return 0;
|
2016-02-12 19:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device id table is used to identify what device can be
|
|
|
|
* supported by this driver
|
|
|
|
*/
|
|
|
|
static const struct i2c_device_id inv_mpu_id[] = {
|
|
|
|
{"mpu6050", INV_MPU6050},
|
|
|
|
{"mpu6500", INV_MPU6500},
|
2018-07-11 09:09:30 +08:00
|
|
|
{"mpu6515", INV_MPU6515},
|
2016-04-20 21:15:13 +08:00
|
|
|
{"mpu9150", INV_MPU9150},
|
2017-03-26 19:11:00 +08:00
|
|
|
{"mpu9250", INV_MPU9250},
|
2018-04-03 06:42:00 +08:00
|
|
|
{"mpu9255", INV_MPU9255},
|
2016-07-01 01:06:34 +08:00
|
|
|
{"icm20608", INV_ICM20608},
|
2020-02-06 18:31:01 +08:00
|
|
|
{"icm20609", INV_ICM20609},
|
|
|
|
{"icm20689", INV_ICM20689},
|
2019-01-29 02:50:03 +08:00
|
|
|
{"icm20602", INV_ICM20602},
|
2020-02-06 18:31:03 +08:00
|
|
|
{"icm20690", INV_ICM20690},
|
2020-02-06 18:31:02 +08:00
|
|
|
{"iam20680", INV_IAM20680},
|
2016-02-12 19:44:44 +08:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
|
|
|
|
|
2017-03-15 12:44:56 +08:00
|
|
|
static const struct of_device_id inv_of_match[] = {
|
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu6050",
|
|
|
|
.data = (void *)INV_MPU6050
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu6500",
|
|
|
|
.data = (void *)INV_MPU6500
|
|
|
|
},
|
2018-07-11 09:09:30 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu6515",
|
|
|
|
.data = (void *)INV_MPU6515
|
|
|
|
},
|
2017-03-15 12:44:56 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu9150",
|
|
|
|
.data = (void *)INV_MPU9150
|
|
|
|
},
|
2017-03-26 19:11:00 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu9250",
|
|
|
|
.data = (void *)INV_MPU9250
|
|
|
|
},
|
2018-04-03 06:42:00 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,mpu9255",
|
|
|
|
.data = (void *)INV_MPU9255
|
|
|
|
},
|
2017-03-15 12:44:56 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,icm20608",
|
|
|
|
.data = (void *)INV_ICM20608
|
|
|
|
},
|
2020-02-06 18:31:01 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,icm20609",
|
|
|
|
.data = (void *)INV_ICM20609
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "invensense,icm20689",
|
|
|
|
.data = (void *)INV_ICM20689
|
|
|
|
},
|
2019-01-29 02:50:03 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,icm20602",
|
|
|
|
.data = (void *)INV_ICM20602
|
|
|
|
},
|
2020-02-06 18:31:03 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,icm20690",
|
|
|
|
.data = (void *)INV_ICM20690
|
|
|
|
},
|
2020-02-06 18:31:02 +08:00
|
|
|
{
|
|
|
|
.compatible = "invensense,iam20680",
|
|
|
|
.data = (void *)INV_IAM20680
|
|
|
|
},
|
2017-03-15 12:44:56 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, inv_of_match);
|
|
|
|
|
2016-02-12 19:44:44 +08:00
|
|
|
static const struct acpi_device_id inv_acpi_match[] = {
|
2016-04-20 21:15:09 +08:00
|
|
|
{"INVN6500", INV_MPU6500},
|
2016-02-12 19:44:44 +08:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
|
|
|
|
|
|
|
|
static struct i2c_driver inv_mpu_driver = {
|
|
|
|
.probe = inv_mpu_probe,
|
|
|
|
.remove = inv_mpu_remove,
|
|
|
|
.id_table = inv_mpu_id,
|
|
|
|
.driver = {
|
2017-03-15 12:44:56 +08:00
|
|
|
.of_match_table = inv_of_match,
|
2016-02-12 19:44:44 +08:00
|
|
|
.acpi_match_table = ACPI_PTR(inv_acpi_match),
|
|
|
|
.name = "inv-mpu6050-i2c",
|
|
|
|
.pm = &inv_mpu_pmops,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_i2c_driver(inv_mpu_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Invensense Corporation");
|
|
|
|
MODULE_DESCRIPTION("Invensense device MPU6050 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|