Skip to content

Commit b3eea8d

Browse files
adrianareusjic23
authored andcommitted
iio: imu: inv_mpu6050: Separate driver into core and i2c functionality.
Separate this driver into core and i2c functionality. This is in preparation for adding spi support. Signed-off-by: Adriana Reus <adriana.reus@intel.com> Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent d430f3c commit b3eea8d

8 files changed

Lines changed: 265 additions & 203 deletions

File tree

‎drivers/iio/imu/inv_mpu6050/Kconfig‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
#
44

55
config INV_MPU6050_IIO
6-
tristate "Invensense MPU6050 devices"
7-
depends on I2C && SYSFS
6+
tristate
87
select IIO_BUFFER
98
select IIO_TRIGGERED_BUFFER
9+
10+
config INV_MPU6050_I2C
11+
tristate "Invensense MPU6050 devices (I2C)"
12+
depends on I2C
13+
select INV_MPU6050_IIO
1014
select I2C_MUX
1115
select REGMAP_I2C
1216
help
@@ -15,4 +19,4 @@ config INV_MPU6050_IIO
1519
and also in MPU6500 mode with some limitations.
1620
It is a gyroscope/accelerometer combo device.
1721
This driver can be built as a module. The module will be called
18-
inv-mpu6050.
22+
inv-mpu6050-i2c.

‎drivers/iio/imu/inv_mpu6050/Makefile‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
#
44

55
obj-$(CONFIG_INV_MPU6050_IIO) += inv-mpu6050.o
6-
inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o inv_mpu_acpi.o
6+
inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o
7+
8+
obj-$(CONFIG_INV_MPU6050_I2C) += inv-mpu6050-i2c.o
9+
inv-mpu6050-i2c-objs := inv_mpu_i2c.o inv_mpu_acpi.o

‎drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,23 @@ static int inv_mpu_process_acpi_config(struct i2c_client *client,
139139
return 0;
140140
}
141141

142-
int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
142+
int inv_mpu_acpi_create_mux_client(struct i2c_client *client)
143143
{
144+
struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
144145

145146
st->mux_client = NULL;
146-
if (ACPI_HANDLE(&st->client->dev)) {
147+
if (ACPI_HANDLE(&client->dev)) {
147148
struct i2c_board_info info;
148149
struct acpi_device *adev;
149150
int ret = -1;
150151

151-
adev = ACPI_COMPANION(&st->client->dev);
152+
adev = ACPI_COMPANION(&client->dev);
152153
memset(&info, 0, sizeof(info));
153154

154155
dmi_check_system(inv_mpu_dev_list);
155156
switch (matched_product_name) {
156157
case INV_MPU_ASUS_T100TA:
157-
ret = asus_acpi_get_sensor_info(adev, st->client,
158+
ret = asus_acpi_get_sensor_info(adev, client,
158159
&info);
159160
break;
160161
/* Add more matched product processing here */
@@ -166,7 +167,7 @@ int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
166167
/* No matching DMI, so create device on INV6XX type */
167168
unsigned short primary, secondary;
168169

169-
ret = inv_mpu_process_acpi_config(st->client, &primary,
170+
ret = inv_mpu_process_acpi_config(client, &primary,
170171
&secondary);
171172
if (!ret && secondary) {
172173
char *name;
@@ -191,21 +192,22 @@ int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
191192
return 0;
192193
}
193194

194-
void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st)
195+
void inv_mpu_acpi_delete_mux_client(struct i2c_client *client)
195196
{
197+
struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
196198
if (st->mux_client)
197199
i2c_unregister_device(st->mux_client);
198200
}
199201
#else
200202

201203
#include "inv_mpu_iio.h"
202204

203-
int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
205+
int inv_mpu_acpi_create_mux_client(struct i2c_client *client)
204206
{
205207
return 0;
206208
}
207209

208-
void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st)
210+
void inv_mpu_acpi_delete_mux_client(struct i2c_client *client)
209211
{
210212
}
211213
#endif

‎drivers/iio/imu/inv_mpu6050/inv_mpu_core.c‎

Lines changed: 25 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
#include <linux/acpi.h>
2828
#include "inv_mpu_iio.h"
2929

30-
static const struct regmap_config inv_mpu_regmap_config = {
31-
.reg_bits = 8,
32-
.val_bits = 8,
33-
};
34-
3530
/*
3631
* this is the gyro scale translated from dynamic range plus/minus
3732
* {250, 500, 1000, 2000} to rad/s
@@ -80,83 +75,6 @@ static const struct inv_mpu6050_hw hw_info[INV_NUM_PARTS] = {
8075
},
8176
};
8277

83-
/*
84-
* The i2c read/write needs to happen in unlocked mode. As the parent
85-
* adapter is common. If we use locked versions, it will fail as
86-
* the mux adapter will lock the parent i2c adapter, while calling
87-
* select/deselect functions.
88-
*/
89-
static int inv_mpu6050_write_reg_unlocked(struct inv_mpu6050_state *st,
90-
u8 reg, u8 d)
91-
{
92-
int ret;
93-
u8 buf[2];
94-
struct i2c_msg msg[1] = {
95-
{
96-
.addr = st->client->addr,
97-
.flags = 0,
98-
.len = sizeof(buf),
99-
.buf = buf,
100-
}
101-
};
102-
103-
buf[0] = reg;
104-
buf[1] = d;
105-
ret = __i2c_transfer(st->client->adapter, msg, 1);
106-
if (ret != 1)
107-
return ret;
108-
109-
return 0;
110-
}
111-
112-
static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
113-
u32 chan_id)
114-
{
115-
struct iio_dev *indio_dev = mux_priv;
116-
struct inv_mpu6050_state *st = iio_priv(indio_dev);
117-
int ret = 0;
118-
119-
/* Use the same mutex which was used everywhere to protect power-op */
120-
mutex_lock(&indio_dev->mlock);
121-
if (!st->powerup_count) {
122-
ret = inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
123-
0);
124-
if (ret)
125-
goto write_error;
126-
127-
msleep(INV_MPU6050_REG_UP_TIME);
128-
}
129-
if (!ret) {
130-
st->powerup_count++;
131-
ret = inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
132-
INV_MPU6050_INT_PIN_CFG |
133-
INV_MPU6050_BIT_BYPASS_EN);
134-
}
135-
write_error:
136-
mutex_unlock(&indio_dev->mlock);
137-
138-
return ret;
139-
}
140-
141-
static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
142-
void *mux_priv, u32 chan_id)
143-
{
144-
struct iio_dev *indio_dev = mux_priv;
145-
struct inv_mpu6050_state *st = iio_priv(indio_dev);
146-
147-
mutex_lock(&indio_dev->mlock);
148-
/* It doesn't really mattter, if any of the calls fails */
149-
inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
150-
INV_MPU6050_INT_PIN_CFG);
151-
st->powerup_count--;
152-
if (!st->powerup_count)
153-
inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
154-
INV_MPU6050_BIT_SLEEP);
155-
mutex_unlock(&indio_dev->mlock);
156-
157-
return 0;
158-
}
159-
16078
int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
16179
{
16280
unsigned int d, mgmt_1;
@@ -758,42 +676,23 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
758676
return 0;
759677
}
760678

761-
/**
762-
* inv_mpu_probe() - probe function.
763-
* @client: i2c client.
764-
* @id: i2c device id.
765-
*
766-
* Returns 0 on success, a negative error code otherwise.
767-
*/
768-
static int inv_mpu_probe(struct i2c_client *client,
769-
const struct i2c_device_id *id)
679+
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name)
770680
{
771681
struct inv_mpu6050_state *st;
772682
struct iio_dev *indio_dev;
773683
struct inv_mpu6050_platform_data *pdata;
684+
struct device *dev = regmap_get_device(regmap);
774685
int result;
775-
struct regmap *regmap;
776686

777-
if (!i2c_check_functionality(client->adapter,
778-
I2C_FUNC_SMBUS_I2C_BLOCK))
779-
return -ENOSYS;
780-
781-
regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
782-
if (IS_ERR(regmap)) {
783-
dev_err(&client->dev, "Failed to register i2c regmap %d\n",
784-
(int)PTR_ERR(regmap));
785-
return PTR_ERR(regmap);
786-
}
787-
788-
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
687+
indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
789688
if (!indio_dev)
790689
return -ENOMEM;
791690

792691
st = iio_priv(indio_dev);
793-
st->client = client;
794692
st->powerup_count = 0;
693+
st->irq = irq;
795694
st->map = regmap;
796-
pdata = dev_get_platdata(&client->dev);
695+
pdata = dev_get_platdata(dev);
797696
if (pdata)
798697
st->plat_data = *pdata;
799698
/* power is turned on inside check chip type*/
@@ -803,18 +702,17 @@ static int inv_mpu_probe(struct i2c_client *client,
803702

804703
result = inv_mpu6050_init_config(indio_dev);
805704
if (result) {
806-
dev_err(&client->dev,
807-
"Could not initialize device.\n");
705+
dev_err(dev, "Could not initialize device.\n");
808706
return result;
809707
}
810708

811-
i2c_set_clientdata(client, indio_dev);
812-
indio_dev->dev.parent = &client->dev;
813-
/* id will be NULL when enumerated via ACPI */
814-
if (id)
815-
indio_dev->name = (char *)id->name;
709+
dev_set_drvdata(dev, indio_dev);
710+
indio_dev->dev.parent = dev;
711+
/* name will be NULL when enumerated via ACPI */
712+
if (name)
713+
indio_dev->name = name;
816714
else
817-
indio_dev->name = (char *)dev_name(&client->dev);
715+
indio_dev->name = dev_name(dev);
818716
indio_dev->channels = inv_mpu_channels;
819717
indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
820718

@@ -826,116 +724,60 @@ static int inv_mpu_probe(struct i2c_client *client,
826724
inv_mpu6050_read_fifo,
827725
NULL);
828726
if (result) {
829-
dev_err(&st->client->dev, "configure buffer fail %d\n",
830-
result);
727+
dev_err(dev, "configure buffer fail %d\n", result);
831728
return result;
832729
}
833730
result = inv_mpu6050_probe_trigger(indio_dev);
834731
if (result) {
835-
dev_err(&st->client->dev, "trigger probe fail %d\n", result);
732+
dev_err(dev, "trigger probe fail %d\n", result);
836733
goto out_unreg_ring;
837734
}
838735

839736
INIT_KFIFO(st->timestamps);
840737
spin_lock_init(&st->time_stamp_lock);
841738
result = iio_device_register(indio_dev);
842739
if (result) {
843-
dev_err(&st->client->dev, "IIO register fail %d\n", result);
740+
dev_err(dev, "IIO register fail %d\n", result);
844741
goto out_remove_trigger;
845742
}
846743

847-
st->mux_adapter = i2c_add_mux_adapter(client->adapter,
848-
&client->dev,
849-
indio_dev,
850-
0, 0, 0,
851-
inv_mpu6050_select_bypass,
852-
inv_mpu6050_deselect_bypass);
853-
if (!st->mux_adapter) {
854-
result = -ENODEV;
855-
goto out_unreg_device;
856-
}
857-
858-
result = inv_mpu_acpi_create_mux_client(st);
859-
if (result)
860-
goto out_del_mux;
861-
862744
return 0;
863745

864-
out_del_mux:
865-
i2c_del_mux_adapter(st->mux_adapter);
866-
out_unreg_device:
867-
iio_device_unregister(indio_dev);
868746
out_remove_trigger:
869747
inv_mpu6050_remove_trigger(st);
870748
out_unreg_ring:
871749
iio_triggered_buffer_cleanup(indio_dev);
872750
return result;
873751
}
752+
EXPORT_SYMBOL_GPL(inv_mpu_core_probe);
874753

875-
static int inv_mpu_remove(struct i2c_client *client)
754+
int inv_mpu_core_remove(struct device *dev)
876755
{
877-
struct iio_dev *indio_dev = i2c_get_clientdata(client);
878-
struct inv_mpu6050_state *st = iio_priv(indio_dev);
756+
struct iio_dev *indio_dev = dev_get_drvdata(dev);
879757

880-
inv_mpu_acpi_delete_mux_client(st);
881-
i2c_del_mux_adapter(st->mux_adapter);
882758
iio_device_unregister(indio_dev);
883-
inv_mpu6050_remove_trigger(st);
759+
inv_mpu6050_remove_trigger(iio_priv(indio_dev));
884760
iio_triggered_buffer_cleanup(indio_dev);
885761

886762
return 0;
887763
}
764+
EXPORT_SYMBOL_GPL(inv_mpu_core_remove);
765+
888766
#ifdef CONFIG_PM_SLEEP
889767

890768
static int inv_mpu_resume(struct device *dev)
891769
{
892-
return inv_mpu6050_set_power_itg(
893-
iio_priv(i2c_get_clientdata(to_i2c_client(dev))), true);
770+
return inv_mpu6050_set_power_itg(iio_priv(dev_get_drvdata(dev)), true);
894771
}
895772

896773
static int inv_mpu_suspend(struct device *dev)
897774
{
898-
return inv_mpu6050_set_power_itg(
899-
iio_priv(i2c_get_clientdata(to_i2c_client(dev))), false);
775+
return inv_mpu6050_set_power_itg(iio_priv(dev_get_drvdata(dev)), false);
900776
}
901-
static SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
902-
903-
#define INV_MPU6050_PMOPS (&inv_mpu_pmops)
904-
#else
905-
#define INV_MPU6050_PMOPS NULL
906777
#endif /* CONFIG_PM_SLEEP */
907778

908-
/*
909-
* device id table is used to identify what device can be
910-
* supported by this driver
911-
*/
912-
static const struct i2c_device_id inv_mpu_id[] = {
913-
{"mpu6050", INV_MPU6050},
914-
{"mpu6500", INV_MPU6500},
915-
{}
916-
};
917-
918-
MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
919-
920-
static const struct acpi_device_id inv_acpi_match[] = {
921-
{"INVN6500", 0},
922-
{ },
923-
};
924-
925-
MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
926-
927-
static struct i2c_driver inv_mpu_driver = {
928-
.probe = inv_mpu_probe,
929-
.remove = inv_mpu_remove,
930-
.id_table = inv_mpu_id,
931-
.driver = {
932-
.name = "inv-mpu6050",
933-
.pm = INV_MPU6050_PMOPS,
934-
.acpi_match_table = ACPI_PTR(inv_acpi_match),
935-
},
936-
};
937-
938-
module_i2c_driver(inv_mpu_driver);
779+
SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
780+
EXPORT_SYMBOL_GPL(inv_mpu_pmops);
939781

940782
MODULE_AUTHOR("Invensense Corporation");
941783
MODULE_DESCRIPTION("Invensense device MPU6050 driver");

0 commit comments

Comments
 (0)