1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/acpi.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/i2c.h>
18#include <linux/iio/iio.h>
19#include <linux/module.h>
20#include <linux/of_device.h>
21#include "inv_mpu_iio.h"
22
23static const struct regmap_config inv_mpu_regmap_config = {
24 .reg_bits = 8,
25 .val_bits = 8,
26};
27
28static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id)
29{
30 struct iio_dev *indio_dev = i2c_mux_priv(muxc);
31 struct inv_mpu6050_state *st = iio_priv(indio_dev);
32 int ret;
33
34 mutex_lock(&st->lock);
35
36 ret = inv_mpu6050_set_power_itg(st, true);
37 if (ret)
38 goto error_unlock;
39
40 ret = regmap_write(st->map, st->reg->int_pin_cfg,
41 st->irq_mask | INV_MPU6050_BIT_BYPASS_EN);
42
43error_unlock:
44 mutex_unlock(&st->lock);
45
46 return ret;
47}
48
49static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id)
50{
51 struct iio_dev *indio_dev = i2c_mux_priv(muxc);
52 struct inv_mpu6050_state *st = iio_priv(indio_dev);
53
54 mutex_lock(&st->lock);
55
56
57 regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
58 inv_mpu6050_set_power_itg(st, false);
59
60 mutex_unlock(&st->lock);
61
62 return 0;
63}
64
65static const char *inv_mpu_match_acpi_device(struct device *dev,
66 enum inv_devices *chip_id)
67{
68 const struct acpi_device_id *id;
69
70 id = acpi_match_device(dev->driver->acpi_match_table, dev);
71 if (!id)
72 return NULL;
73
74 *chip_id = (int)id->driver_data;
75
76 return dev_name(dev);
77}
78
79
80
81
82
83
84
85
86static int inv_mpu_probe(struct i2c_client *client,
87 const struct i2c_device_id *id)
88{
89 struct inv_mpu6050_state *st;
90 int result;
91 enum inv_devices chip_type;
92 struct regmap *regmap;
93 const char *name;
94
95 if (!i2c_check_functionality(client->adapter,
96 I2C_FUNC_SMBUS_I2C_BLOCK))
97 return -EOPNOTSUPP;
98
99 if (client->dev.of_node) {
100 chip_type = (enum inv_devices)
101 of_device_get_match_data(&client->dev);
102 name = client->name;
103 } else if (id) {
104 chip_type = (enum inv_devices)
105 id->driver_data;
106 name = id->name;
107 } else if (ACPI_HANDLE(&client->dev)) {
108 name = inv_mpu_match_acpi_device(&client->dev, &chip_type);
109 if (!name)
110 return -ENODEV;
111 } else {
112 return -ENOSYS;
113 }
114
115 regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
116 if (IS_ERR(regmap)) {
117 dev_err(&client->dev, "Failed to register i2c regmap %d\n",
118 (int)PTR_ERR(regmap));
119 return PTR_ERR(regmap);
120 }
121
122 result = inv_mpu_core_probe(regmap, client->irq, name,
123 NULL, chip_type);
124 if (result < 0)
125 return result;
126
127 st = iio_priv(dev_get_drvdata(&client->dev));
128 switch (st->chip_type) {
129 case INV_ICM20608:
130
131 break;
132 default:
133
134 st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
135 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
136 inv_mpu6050_select_bypass,
137 inv_mpu6050_deselect_bypass);
138 if (!st->muxc)
139 return -ENOMEM;
140 st->muxc->priv = dev_get_drvdata(&client->dev);
141 result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
142 if (result)
143 return result;
144 result = inv_mpu_acpi_create_mux_client(client);
145 if (result)
146 goto out_del_mux;
147 break;
148 }
149
150 return 0;
151
152out_del_mux:
153 i2c_mux_del_adapters(st->muxc);
154 return result;
155}
156
157static int inv_mpu_remove(struct i2c_client *client)
158{
159 struct iio_dev *indio_dev = i2c_get_clientdata(client);
160 struct inv_mpu6050_state *st = iio_priv(indio_dev);
161
162 if (st->muxc) {
163 inv_mpu_acpi_delete_mux_client(client);
164 i2c_mux_del_adapters(st->muxc);
165 }
166
167 return 0;
168}
169
170
171
172
173
174static const struct i2c_device_id inv_mpu_id[] = {
175 {"mpu6050", INV_MPU6050},
176 {"mpu6500", INV_MPU6500},
177 {"mpu6515", INV_MPU6515},
178 {"mpu9150", INV_MPU9150},
179 {"mpu9250", INV_MPU9250},
180 {"mpu9255", INV_MPU9255},
181 {"icm20608", INV_ICM20608},
182 {}
183};
184
185MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
186
187static const struct of_device_id inv_of_match[] = {
188 {
189 .compatible = "invensense,mpu6050",
190 .data = (void *)INV_MPU6050
191 },
192 {
193 .compatible = "invensense,mpu6500",
194 .data = (void *)INV_MPU6500
195 },
196 {
197 .compatible = "invensense,mpu6515",
198 .data = (void *)INV_MPU6515
199 },
200 {
201 .compatible = "invensense,mpu9150",
202 .data = (void *)INV_MPU9150
203 },
204 {
205 .compatible = "invensense,mpu9250",
206 .data = (void *)INV_MPU9250
207 },
208 {
209 .compatible = "invensense,mpu9255",
210 .data = (void *)INV_MPU9255
211 },
212 {
213 .compatible = "invensense,icm20608",
214 .data = (void *)INV_ICM20608
215 },
216 { }
217};
218MODULE_DEVICE_TABLE(of, inv_of_match);
219
220static const struct acpi_device_id inv_acpi_match[] = {
221 {"INVN6500", INV_MPU6500},
222 { },
223};
224
225MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
226
227static struct i2c_driver inv_mpu_driver = {
228 .probe = inv_mpu_probe,
229 .remove = inv_mpu_remove,
230 .id_table = inv_mpu_id,
231 .driver = {
232 .of_match_table = inv_of_match,
233 .acpi_match_table = ACPI_PTR(inv_acpi_match),
234 .name = "inv-mpu6050-i2c",
235 .pm = &inv_mpu_pmops,
236 },
237};
238
239module_i2c_driver(inv_mpu_driver);
240
241MODULE_AUTHOR("Invensense Corporation");
242MODULE_DESCRIPTION("Invensense device MPU6050 driver");
243MODULE_LICENSE("GPL");
244