Skip to content

Commit fd64df1

Browse files
adrianareusjic23
authored andcommitted
iio: imu: inv_mpu6050: Add SPI support for MPU6000
The only difference between the MPU6000 and the MPU6050 is that the first also supports SPI. Add SPI driver for this chip. 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 b3eea8d commit fd64df1

6 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎drivers/iio/imu/inv_mpu6050/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ config INV_MPU6050_I2C
2020
It is a gyroscope/accelerometer combo device.
2121
This driver can be built as a module. The module will be called
2222
inv-mpu6050-i2c.
23+
24+
config INV_MPU6050_SPI
25+
tristate "Invensense MPU6050 devices (SPI)"
26+
depends on SPI_MASTER
27+
select INV_MPU6050_IIO
28+
select REGMAP_SPI
29+
help
30+
This driver supports the Invensense MPU6050 devices.
31+
It is a gyroscope/accelerometer combo device.
32+
This driver can be built as a module. The module will be called
33+
inv-mpu6050-spi.

‎drivers/iio/imu/inv_mpu6050/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o
77

88
obj-$(CONFIG_INV_MPU6050_I2C) += inv-mpu6050-i2c.o
99
inv-mpu6050-i2c-objs := inv_mpu_i2c.o inv_mpu_acpi.o
10+
11+
obj-$(CONFIG_INV_MPU6050_SPI) += inv-mpu6050-spi.o
12+
inv-mpu6050-spi-objs := inv_mpu_spi.o

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
152152

153153
return 0;
154154
}
155+
EXPORT_SYMBOL_GPL(inv_mpu6050_set_power_itg);
155156

156157
/**
157158
* inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
@@ -676,7 +677,8 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
676677
return 0;
677678
}
678679

679-
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name)
680+
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
681+
int (*inv_mpu_bus_setup)(struct iio_dev *))
680682
{
681683
struct inv_mpu6050_state *st;
682684
struct iio_dev *indio_dev;
@@ -700,6 +702,9 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name)
700702
if (result)
701703
return result;
702704

705+
if (inv_mpu_bus_setup)
706+
inv_mpu_bus_setup(indio_dev);
707+
703708
result = inv_mpu6050_init_config(indio_dev);
704709
if (result) {
705710
dev_err(dev, "Could not initialize device.\n");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static int inv_mpu_probe(struct i2c_client *client,
129129
return PTR_ERR(regmap);
130130
}
131131

132-
result = inv_mpu_core_probe(regmap, client->irq, name);
132+
result = inv_mpu_core_probe(regmap, client->irq, name, NULL);
133133
if (result < 0)
134134
return result;
135135

‎drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct inv_mpu6050_reg_map {
6262
enum inv_devices {
6363
INV_MPU6050,
6464
INV_MPU6500,
65+
INV_MPU6000,
6566
INV_NUM_PARTS
6667
};
6768

@@ -154,6 +155,7 @@ struct inv_mpu6050_state {
154155
#define INV_MPU6050_BIT_I2C_MST_EN 0x20
155156
#define INV_MPU6050_BIT_FIFO_EN 0x40
156157
#define INV_MPU6050_BIT_DMP_EN 0x80
158+
#define INV_MPU6050_BIT_I2C_IF_DIS 0x10
157159

158160
#define INV_MPU6050_REG_PWR_MGMT_1 0x6B
159161
#define INV_MPU6050_BIT_H_RESET 0x80
@@ -258,6 +260,8 @@ int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
258260
int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
259261
int inv_mpu_acpi_create_mux_client(struct i2c_client *client);
260262
void inv_mpu_acpi_delete_mux_client(struct i2c_client *client);
261-
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name);
263+
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
264+
int (*inv_mpu_bus_setup)(struct iio_dev *));
262265
int inv_mpu_core_remove(struct device *dev);
266+
int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
263267
extern const struct dev_pm_ops inv_mpu_pmops;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2015 Intel Corporation Inc.
3+
*
4+
* This software is licensed under the terms of the GNU General Public
5+
* License version 2, as published by the Free Software Foundation, and
6+
* may be copied, distributed, and modified under those terms.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
#include <linux/module.h>
14+
#include <linux/acpi.h>
15+
#include <linux/spi/spi.h>
16+
#include <linux/regmap.h>
17+
#include <linux/iio/iio.h>
18+
#include "inv_mpu_iio.h"
19+
20+
static const struct regmap_config inv_mpu_regmap_config = {
21+
.reg_bits = 8,
22+
.val_bits = 8,
23+
};
24+
25+
static int inv_mpu_i2c_disable(struct iio_dev *indio_dev)
26+
{
27+
struct inv_mpu6050_state *st = iio_priv(indio_dev);
28+
int ret = 0;
29+
30+
ret = inv_mpu6050_set_power_itg(st, true);
31+
if (ret)
32+
return ret;
33+
34+
ret = regmap_write(st->map, INV_MPU6050_REG_USER_CTRL,
35+
INV_MPU6050_BIT_I2C_IF_DIS);
36+
if (ret) {
37+
inv_mpu6050_set_power_itg(st, false);
38+
return ret;
39+
}
40+
41+
return inv_mpu6050_set_power_itg(st, false);
42+
}
43+
44+
static int inv_mpu_probe(struct spi_device *spi)
45+
{
46+
struct regmap *regmap;
47+
const struct spi_device_id *id = spi_get_device_id(spi);
48+
const char *name = id ? id->name : NULL;
49+
50+
regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config);
51+
if (IS_ERR(regmap)) {
52+
dev_err(&spi->dev, "Failed to register spi regmap %d\n",
53+
(int)PTR_ERR(regmap));
54+
return PTR_ERR(regmap);
55+
}
56+
57+
return inv_mpu_core_probe(regmap, spi->irq, name, inv_mpu_i2c_disable);
58+
}
59+
60+
static int inv_mpu_remove(struct spi_device *spi)
61+
{
62+
return inv_mpu_core_remove(&spi->dev);
63+
}
64+
65+
/*
66+
* device id table is used to identify what device can be
67+
* supported by this driver
68+
*/
69+
static const struct spi_device_id inv_mpu_id[] = {
70+
{"mpu6000", INV_MPU6000},
71+
{}
72+
};
73+
74+
MODULE_DEVICE_TABLE(spi, inv_mpu_id);
75+
76+
static const struct acpi_device_id inv_acpi_match[] = {
77+
{"INVN6000", 0},
78+
{ },
79+
};
80+
MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
81+
82+
static struct spi_driver inv_mpu_driver = {
83+
.probe = inv_mpu_probe,
84+
.remove = inv_mpu_remove,
85+
.id_table = inv_mpu_id,
86+
.driver = {
87+
.acpi_match_table = ACPI_PTR(inv_acpi_match),
88+
.name = "inv-mpu6000-spi",
89+
.pm = &inv_mpu_pmops,
90+
},
91+
};
92+
93+
module_spi_driver(inv_mpu_driver);
94+
95+
MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
96+
MODULE_DESCRIPTION("Invensense device MPU6000 driver");
97+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)