Re: [RFC PATCH 4/5] iio: imu: inv_mpu6050: Separate driver into core and i2c functionality.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 30/01/16 09:43, Adriana Reus wrote:
> Separate this driver into core and i2c functionality.
> This is in preparation for adding spi support.
> 
> Signed-off-by: Adriana Reus <adriana.reus@xxxxxxxxx>
> ---
>  drivers/iio/imu/inv_mpu6050/Kconfig           |  11 +-
>  drivers/iio/imu/inv_mpu6050/Makefile          |   5 +-
>  drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c    |  14 +-
>  drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    | 209 ++++----------------------
>  drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c     | 209 ++++++++++++++++++++++++++
>  drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h     |  11 +-
>  drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c    |   4 +-
>  drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c |   4 +-
>  8 files changed, 267 insertions(+), 200 deletions(-)
>  create mode 100644 drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> 
> diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig
> index f301f3a..2225d3c 100644
> --- a/drivers/iio/imu/inv_mpu6050/Kconfig
> +++ b/drivers/iio/imu/inv_mpu6050/Kconfig
> @@ -4,11 +4,10 @@
>  
>  config INV_MPU6050_IIO
>  	tristate "Invensense MPU6050 devices"
> -	depends on I2C && SYSFS
> +	depends on SYSFS
>  	select IIO_BUFFER
>  	select IIO_TRIGGERED_BUFFER
> -	select I2C_MUX
> -	select REGMAP_I2C
> +	select INV_MPU6050_I2C if I2C
>  	help
>  	  This driver supports the Invensense MPU6050 devices.
>  	  This driver can also support MPU6500 in MPU6050 compatibility mode
> @@ -16,3 +15,9 @@ config INV_MPU6050_IIO
>  	  It is a gyroscope/accelerometer combo device.
>  	  This driver can be built as a module. The module will be called
>  	  inv-mpu6050.
> +
> +config INV_MPU6050_I2C
> +	tristate "Invensense MPU6050 devices"
> +	depends on I2C
> +	select I2C_MUX
> +	select REGMAP_I2C
> \ No newline at end of file
Fix this by adding one.

I think I prefer the inverted option on this that we seem to be moving
towards with the core being 'hidden' and the i2c / spi drivers explicitly
selected. Lars gave an explanation of this the other day in the mpl115
driver review he did;

....

Hi,

for combined SPI and I2C support please follow a scheme similar to that used
by the mma7455 driver
(http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/iio?id=a84ef0d181d917125f1f16cffe53f84c19968969)
and split the driver into a core support module and one module each for I2C
and SPI support.

That is a much cleaner approach and avoids issues that can happen if core
I2C support is build as a module and core SPI support is built-in.

- Lars

....

So we have lots of drivers doing it the other way around, with rather fiddly
dependencies and selects and should probably unwind that at some point!

Otherwise, looks fairly straight forward to me.  A few bits and bobs inline.

Jonathan
> diff --git a/drivers/iio/imu/inv_mpu6050/Makefile b/drivers/iio/imu/inv_mpu6050/Makefile
> index f566f6a..cb5df02 100644
> --- a/drivers/iio/imu/inv_mpu6050/Makefile
> +++ b/drivers/iio/imu/inv_mpu6050/Makefile
> @@ -3,4 +3,7 @@
>  #
>  
>  obj-$(CONFIG_INV_MPU6050_IIO) += inv-mpu6050.o
> -inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o inv_mpu_acpi.o
> +inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o
> +
> +obj-$(CONFIG_INV_MPU6050_I2C) += inv-mpu6050-i2c.o
> +inv-mpu6050-i2c-objs := inv_mpu_i2c.o inv_mpu_acpi.o
add one.
> \ No newline at end of file
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c
> index 1c982a5..72f6cdf 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c
> @@ -139,22 +139,23 @@ static int inv_mpu_process_acpi_config(struct i2c_client *client,
>  	return 0;
>  }
>  
> -int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
> +int inv_mpu_acpi_create_mux_client(struct i2c_client *client)
>  {
> +	struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
>  
>  	st->mux_client = NULL;
> -	if (ACPI_HANDLE(&st->client->dev)) {
> +	if (ACPI_HANDLE(st->dev)) {
>  		struct i2c_board_info info;
>  		struct acpi_device *adev;
>  		int ret = -1;
>  
> -		adev = ACPI_COMPANION(&st->client->dev);
> +		adev = ACPI_COMPANION(st->dev);
>  		memset(&info, 0, sizeof(info));
>  
>  		dmi_check_system(inv_mpu_dev_list);
>  		switch (matched_product_name) {
>  		case INV_MPU_ASUS_T100TA:
> -			ret = asus_acpi_get_sensor_info(adev, st->client,
> +			ret = asus_acpi_get_sensor_info(adev, client,
>  							&info);
>  			break;
>  		/* Add more matched product processing here */
> @@ -166,7 +167,7 @@ int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
>  			/* No matching DMI, so create device on INV6XX type */
>  			unsigned short primary, secondary;
>  
> -			ret = inv_mpu_process_acpi_config(st->client, &primary,
> +			ret = inv_mpu_process_acpi_config(client, &primary,
>  							  &secondary);
>  			if (!ret && secondary) {
>  				char *name;
> @@ -191,8 +192,9 @@ int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
>  	return 0;
>  }
>  
> -void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st)
> +void inv_mpu_acpi_delete_mux_client(struct i2c_client *client)
>  {
> +	struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
>  	if (st->mux_client)
>  		i2c_unregister_device(st->mux_client);
>  }
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
> index 151a378..803fa87 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
> @@ -27,11 +27,6 @@
>  #include <linux/acpi.h>
>  #include "inv_mpu_iio.h"
>  
> -static const struct regmap_config inv_mpu_regmap_config = {
> -	.reg_bits = 8,
> -	.val_bits = 8,
> -};
> -
>  /*
>   * this is the gyro scale translated from dynamic range plus/minus
>   * {250, 500, 1000, 2000} to rad/s
> @@ -80,83 +75,6 @@ static const struct inv_mpu6050_hw hw_info[INV_NUM_PARTS] = {
>  	},
>  };
>  
> -/*
> - * The i2c read/write needs to happen in unlocked mode. As the parent
> - * adapter is common. If we use locked versions, it will fail as
> - * the mux adapter will lock the parent i2c adapter, while calling
> - * select/deselect functions.
> - */
> -static int inv_mpu6050_write_reg_unlocked(struct inv_mpu6050_state *st,
> -					  u8 reg, u8 d)
> -{
> -	int ret;
> -	u8 buf[2];
> -	struct i2c_msg msg[1] = {
> -		{
> -			.addr = st->client->addr,
> -			.flags = 0,
> -			.len = sizeof(buf),
> -			.buf = buf,
> -		}
> -	};
> -
> -	buf[0] = reg;
> -	buf[1] = d;
> -	ret = __i2c_transfer(st->client->adapter, msg, 1);
> -	if (ret != 1)
> -		return ret;
> -
> -	return 0;
> -}
> -
> -static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
> -				     u32 chan_id)
> -{
> -	struct iio_dev *indio_dev = mux_priv;
> -	struct inv_mpu6050_state *st = iio_priv(indio_dev);
> -	int ret = 0;
> -
> -	/* Use the same mutex which was used everywhere to protect power-op */
> -	mutex_lock(&indio_dev->mlock);
> -	if (!st->powerup_count) {
> -		ret = inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
> -						     0);
> -		if (ret)
> -			goto write_error;
> -
> -		msleep(INV_MPU6050_REG_UP_TIME);
> -	}
> -	if (!ret) {
> -		st->powerup_count++;
> -		ret = inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
> -						     INV_MPU6050_INT_PIN_CFG |
> -						     INV_MPU6050_BIT_BYPASS_EN);
> -	}
> -write_error:
> -	mutex_unlock(&indio_dev->mlock);
> -
> -	return ret;
> -}
> -
> -static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
> -				       void *mux_priv, u32 chan_id)
> -{
> -	struct iio_dev *indio_dev = mux_priv;
> -	struct inv_mpu6050_state *st = iio_priv(indio_dev);
> -
> -	mutex_lock(&indio_dev->mlock);
> -	/* It doesn't really mattter, if any of the calls fails */
> -	inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
> -				       INV_MPU6050_INT_PIN_CFG);
> -	st->powerup_count--;
> -	if (!st->powerup_count)
> -		inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
> -					       INV_MPU6050_BIT_SLEEP);
> -	mutex_unlock(&indio_dev->mlock);
> -
> -	return 0;
> -}
> -
>  int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
>  {
>  	unsigned int d, mgmt_1;
> @@ -758,42 +676,24 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
>  	return 0;
>  }
>  
> -/**
> - *  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,
> -	const struct i2c_device_id *id)
> +int inv_mpu_core_probe(struct device *dev, struct regmap *regmap, int irq,
> +		       const char *name)
>  {
>  	struct inv_mpu6050_state *st;
>  	struct iio_dev *indio_dev;
>  	struct inv_mpu6050_platform_data *pdata;
>  	int result;
> -	struct regmap *regmap;
>  
> -	if (!i2c_check_functionality(client->adapter,
> -		I2C_FUNC_SMBUS_I2C_BLOCK))
> -		return -ENOSYS;
> -
> -	regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
> -	if (IS_ERR(regmap)) {
> -		dev_err(&client->dev, "Failed to register i2c regmap %d\n",
> -			(int)PTR_ERR(regmap));
> -		return PTR_ERR(regmap);
> -	}
> -
> -	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
>  	if (!indio_dev)
>  		return -ENOMEM;
>  
>  	st = iio_priv(indio_dev);
> -	st->client = client;
>  	st->powerup_count = 0;
> +	st->dev = dev;
> +	st->irq = irq;
>  	st->map = regmap;
> -	pdata = dev_get_platdata(&client->dev);
> +	pdata = dev_get_platdata(dev);
>  	if (pdata)
>  		st->plat_data = *pdata;
>  	/* power is turned on inside check chip type*/
> @@ -803,18 +703,17 @@ static int inv_mpu_probe(struct i2c_client *client,
>  
>  	result = inv_mpu6050_init_config(indio_dev);
>  	if (result) {
> -		dev_err(&client->dev,
> -			"Could not initialize device.\n");
> +		dev_err(dev, "Could not initialize device.\n");
>  		return result;
>  	}
>  
> -	i2c_set_clientdata(client, indio_dev);
> -	indio_dev->dev.parent = &client->dev;
> -	/* id will be NULL when enumerated via ACPI */
> -	if (id)
> -		indio_dev->name = (char *)id->name;
> +	dev_set_drvdata(dev, indio_dev);
> +	indio_dev->dev.parent = dev;
> +	/* name will be NULL when enumerated via ACPI */
> +	if (name)
> +		indio_dev->name = (char *)name;
>  	else
> -		indio_dev->name = (char *)dev_name(&client->dev);
> +		indio_dev->name = (char *)dev_name(dev);
>  	indio_dev->channels = inv_mpu_channels;
>  	indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
>  
> @@ -826,13 +725,12 @@ static int inv_mpu_probe(struct i2c_client *client,
>  					    inv_mpu6050_read_fifo,
>  					    NULL);
>  	if (result) {
> -		dev_err(&st->client->dev, "configure buffer fail %d\n",
> -				result);
> +		dev_err(dev, "configure buffer fail %d\n", result);
>  		return result;
>  	}
>  	result = inv_mpu6050_probe_trigger(indio_dev);
>  	if (result) {
> -		dev_err(&st->client->dev, "trigger probe fail %d\n", result);
> +		dev_err(dev, "trigger probe fail %d\n", result);
>  		goto out_unreg_ring;
>  	}
>  
> @@ -840,102 +738,47 @@ static int inv_mpu_probe(struct i2c_client *client,
>  	spin_lock_init(&st->time_stamp_lock);
>  	result = iio_device_register(indio_dev);
>  	if (result) {
> -		dev_err(&st->client->dev, "IIO register fail %d\n", result);
> +		dev_err(dev, "IIO register fail %d\n", result);
>  		goto out_remove_trigger;
>  	}
>  
> -	st->mux_adapter = i2c_add_mux_adapter(client->adapter,
> -					      &client->dev,
> -					      indio_dev,
> -					      0, 0, 0,
> -					      inv_mpu6050_select_bypass,
> -					      inv_mpu6050_deselect_bypass);
> -	if (!st->mux_adapter) {
> -		result = -ENODEV;
> -		goto out_unreg_device;
> -	}
> -
> -	result = inv_mpu_acpi_create_mux_client(st);
> -	if (result)
> -		goto out_del_mux;
> -
>  	return 0;
>  
> -out_del_mux:
> -	i2c_del_mux_adapter(st->mux_adapter);
> -out_unreg_device:
> -	iio_device_unregister(indio_dev);
>  out_remove_trigger:
>  	inv_mpu6050_remove_trigger(st);
>  out_unreg_ring:
>  	iio_triggered_buffer_cleanup(indio_dev);
>  	return result;
>  }
> +EXPORT_SYMBOL_GPL(inv_mpu_core_probe);
>  
> -static int inv_mpu_remove(struct i2c_client *client)
> +int inv_mpu_core_remove(struct device  *dev)
>  {
> -	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> -	struct inv_mpu6050_state *st = iio_priv(indio_dev);
> +	struct iio_dev *indio_dev = dev_get_drvdata(dev);
>  
> -	inv_mpu_acpi_delete_mux_client(st);
> -	i2c_del_mux_adapter(st->mux_adapter);
>  	iio_device_unregister(indio_dev);
> -	inv_mpu6050_remove_trigger(st);
> +	inv_mpu6050_remove_trigger(iio_priv(indio_dev));
>  	iio_triggered_buffer_cleanup(indio_dev);
>  
>  	return 0;
>  }
> +EXPORT_SYMBOL_GPL(inv_mpu_core_remove);
> +
>  #ifdef CONFIG_PM_SLEEP
>  
>  static int inv_mpu_resume(struct device *dev)
>  {
> -	return inv_mpu6050_set_power_itg(
> -		iio_priv(i2c_get_clientdata(to_i2c_client(dev))), true);
> +	return inv_mpu6050_set_power_itg(iio_priv(dev_get_drvdata(dev)), true);
>  }
>  
>  static int inv_mpu_suspend(struct device *dev)
>  {
> -	return inv_mpu6050_set_power_itg(
> -		iio_priv(i2c_get_clientdata(to_i2c_client(dev))), false);
> +	return inv_mpu6050_set_power_itg(iio_priv(dev_get_drvdata(dev)), false);
>  }
> -static SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
> -
> -#define INV_MPU6050_PMOPS (&inv_mpu_pmops)
> -#else
> -#define INV_MPU6050_PMOPS NULL
>  #endif /* CONFIG_PM_SLEEP */
>  
> -/*
> - * 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},
> -	{}
> -};
> -
> -MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
> -
> -static const struct acpi_device_id inv_acpi_match[] = {
> -	{"INVN6500", 0},
> -	{ },
> -};
> -
> -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 = {
> -		.name	=	"inv-mpu6050",
> -		.pm     =       INV_MPU6050_PMOPS,
> -		.acpi_match_table = ACPI_PTR(inv_acpi_match),
> -	},
> -};
> -
> -module_i2c_driver(inv_mpu_driver);
> +SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
> +EXPORT_SYMBOL_GPL(inv_mpu_pmops);
>  
>  MODULE_AUTHOR("Invensense Corporation");
>  MODULE_DESCRIPTION("Invensense device MPU6050 driver");
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> new file mode 100644
> index 0000000..7580948
> --- /dev/null
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> @@ -0,0 +1,209 @@
> +/*
> +* Copyright (C) 2012 Invensense, Inc.
> +*
> +* This software is licensed under the terms of the GNU General Public
> +* License version 2, as published by the Free Software Foundation, and
> +* may be copied, distributed, and modified under those terms.
> +*
> +* This program is distributed in the hope that it will be useful,
> +* but WITHOUT ANY WARRANTY; without even the implied warranty of
> +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +* GNU General Public License for more details.
> +*/
> +
> +#include <linux/acpi.h>
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/i2c.h>
> +#include <linux/i2c-mux.h>
> +#include <linux/iio/iio.h>
> +#include <linux/module.h>
> +#include "inv_mpu_iio.h"
> +
> +static const struct regmap_config inv_mpu_regmap_config = {
> +	.reg_bits = 8,
> +	.val_bits = 8,
> +};
> +
> +/*
> + * The i2c read/write needs to happen in unlocked mode. As the parent
> + * adapter is common. If we use locked versions, it will fail as
> + * the mux adapter will lock the parent i2c adapter, while calling
> + * select/deselect functions.
> + */
> +static int inv_mpu6050_write_reg_unlocked(struct i2c_client *client,
> +					  u8 reg, u8 d)
> +{
> +	int ret;
> +	u8 buf[2];
> +	struct i2c_msg msg[1] = {
> +		{
> +			.addr = client->addr,
> +			.flags = 0,
> +			.len = sizeof(buf),
> +			.buf = buf,
> +		}
> +	};
> +
> +	buf[0] = reg;
> +	buf[1] = d;
Guess this is cut and paste, but might as well fill buf directly above
u8 buf[] = {reg, d};
> +	ret = __i2c_transfer(client->adapter, msg, 1);
> +	if (ret != 1)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
> +				     u32 chan_id)
> +{
> +	struct i2c_client *client = mux_priv;
> +	struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
> +	struct inv_mpu6050_state *st = iio_priv(indio_dev);
> +	int ret = 0;
> +
> +	/* Use the same mutex which was used everywhere to protect power-op */
> +	mutex_lock(&indio_dev->mlock);
> +	if (!st->powerup_count) {
> +		ret = inv_mpu6050_write_reg_unlocked(client,
> +						     st->reg->pwr_mgmt_1, 0);
> +		if (ret)
> +			goto write_error;
> +
> +		msleep(INV_MPU6050_REG_UP_TIME);
> +	}
> +	if (!ret) {
> +		st->powerup_count++;
> +		ret = inv_mpu6050_write_reg_unlocked(client,
> +						     st->reg->int_pin_cfg,
> +						     INV_MPU6050_INT_PIN_CFG |
> +						     INV_MPU6050_BIT_BYPASS_EN);
> +	}
> +write_error:
> +	mutex_unlock(&indio_dev->mlock);
> +
> +	return ret;
> +}
> +
> +static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
> +				       void *mux_priv, u32 chan_id)
> +{
> +	struct i2c_client *client = mux_priv;
> +	struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
> +	struct inv_mpu6050_state *st = iio_priv(indio_dev);
> +
> +	mutex_lock(&indio_dev->mlock);
> +	/* It doesn't really mattter, if any of the calls fails */
> +	inv_mpu6050_write_reg_unlocked(client, st->reg->int_pin_cfg,
> +				       INV_MPU6050_INT_PIN_CFG);
> +	st->powerup_count--;
> +	if (!st->powerup_count)
> +		inv_mpu6050_write_reg_unlocked(client, st->reg->pwr_mgmt_1,
> +					       INV_MPU6050_BIT_SLEEP);
> +	mutex_unlock(&indio_dev->mlock);
> +
> +	return 0;
> +}
> +
> +/**
> + *  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,
> +	const struct i2c_device_id *id)
> +{
> +	struct inv_mpu6050_state *st;
> +	int result;
> +	const char *name = id ? id->name : NULL;
> +	struct regmap *regmap;
> +
> +	if (!i2c_check_functionality(client->adapter,
> +				     I2C_FUNC_SMBUS_I2C_BLOCK))
> +		return -ENOSYS;
> +
> +	regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
> +	if (IS_ERR(regmap)) {
> +		dev_err(&client->dev, "Failed to register i2c regmap %d\n",
> +			(int)PTR_ERR(regmap));
> +		return PTR_ERR(regmap);
> +	}
> +
> +	result = inv_mpu_core_probe(&client->dev, regmap, client->irq, name);
> +	if (result < 0)
> +		return result;
> +
> +	st = iio_priv(dev_get_drvdata(&client->dev));
> +	st->mux_adapter = i2c_add_mux_adapter(client->adapter,
> +					      &client->dev,
> +					      client,
> +					      0, 0, 0,
> +					      inv_mpu6050_select_bypass,
> +					      inv_mpu6050_deselect_bypass);
> +	if (!st->mux_adapter) {
> +		result = -ENODEV;
> +		goto out_unreg_device;
> +	}
> +
> +	result = inv_mpu_acpi_create_mux_client(client);
> +	if (result)
> +		goto out_del_mux;
> +
> +	return 0;
> +
> +out_del_mux:
> +	i2c_del_mux_adapter(st->mux_adapter);
> +out_unreg_device:
> +	inv_mpu_core_remove(&client->dev);
> +	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);
> +
> +	inv_mpu_acpi_delete_mux_client(client);
> +	i2c_del_mux_adapter(st->mux_adapter);
> +
> +	return inv_mpu_core_remove(&client->dev);
> +}
> +
> +/*
> + * 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},
> +	{}
> +};
> +
> +MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
> +
> +static const struct acpi_device_id inv_acpi_match[] = {
> +	{"INVN6500", 0},
> +	{ },
> +};
> +
> +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 = {
> +		.owner	=	THIS_MODULE,
> +		.acpi_match_table = ACPI_PTR(inv_acpi_match),
> +		.name	=	"inv-mpu6050 i2c driver",
> +		.pm     =       &inv_mpu_pmops,
> +	},
> +};
> +
> +module_i2c_driver(inv_mpu_driver);
> +
> +MODULE_AUTHOR("Invensense Corporation");
> +MODULE_DESCRIPTION("Invensense device MPU6050 driver");
> +MODULE_LICENSE("GPL");
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
> index 469cd1f..0ef6046 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
> @@ -120,13 +120,14 @@ struct inv_mpu6050_state {
>  	const struct inv_mpu6050_hw *hw;
>  	enum   inv_devices chip_type;
>  	spinlock_t time_stamp_lock;
> -	struct i2c_client *client;
>  	struct i2c_adapter *mux_adapter;
>  	struct i2c_client *mux_client;
>  	unsigned int powerup_count;
>  	struct inv_mpu6050_platform_data plat_data;
>  	DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
>  	struct regmap *map;
> +	struct device *dev;
Had a discussion about this in a review the other day.  You can
retrieve the dev from the regmap with: regmap_get_device
and hence avoid having another copy of the pointer here.

> +	int irq;
>  };
>  
>  /*register and associated bit definition*/
> @@ -255,5 +256,9 @@ int inv_reset_fifo(struct iio_dev *indio_dev);
>  int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask);
>  int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
>  int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
> -int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st);
> -void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st);
> +int inv_mpu_acpi_create_mux_client(struct i2c_client *client);
> +void inv_mpu_acpi_delete_mux_client(struct i2c_client *client);
> +int inv_mpu_core_probe(struct device *dev, struct regmap *regmap, int irq,
> +		       const char *name);
> +int inv_mpu_core_remove(struct device *dev);
> +extern const struct dev_pm_ops inv_mpu_pmops;
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
> index eb19dae..f02f4eb 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
> @@ -42,7 +42,7 @@ int inv_reset_fifo(struct iio_dev *indio_dev)
>  	/* disable interrupt */
>  	result = regmap_write(st->map, st->reg->int_enable, 0);
>  	if (result) {
> -		dev_err(&st->client->dev, "int_enable failed %d\n", result);
> +		dev_err(st->dev, "int_enable failed %d\n", result);
>  		return result;
>  	}
>  	/* disable the sensor output to FIFO */
> @@ -89,7 +89,7 @@ int inv_reset_fifo(struct iio_dev *indio_dev)
>  	return 0;
>  
>  reset_fifo_fail:
> -	dev_err(&st->client->dev, "reset fifo failed %d\n", result);
> +	dev_err(st->dev, "reset fifo failed %d\n", result);
>  	result = regmap_write(st->map, st->reg->int_enable,
>  					INV_MPU6050_BIT_DATA_RDY_EN);
>  
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
> index ee9e66d..9e00484 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
> @@ -123,7 +123,7 @@ int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev)
>  	if (!st->trig)
>  		return -ENOMEM;
>  
> -	ret = devm_request_irq(&indio_dev->dev, st->client->irq,
> +	ret = devm_request_irq(&indio_dev->dev, st->irq,
>  			       &iio_trigger_generic_data_rdy_poll,
>  			       IRQF_TRIGGER_RISING,
>  			       "inv_mpu",
> @@ -131,7 +131,7 @@ int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev)
>  	if (ret)
>  		return ret;
>  
> -	st->trig->dev.parent = &st->client->dev;
> +	st->trig->dev.parent = st->dev;
>  	st->trig->ops = &inv_mpu_trigger_ops;
>  	iio_trigger_set_drvdata(st->trig, indio_dev);
>  
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux