Commit | Line | Data |
---|---|---|
b3eea8da AR |
1 | /* |
2 | * Copyright (C) 2012 Invensense, 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 | ||
14 | #include <linux/acpi.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/err.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/i2c-mux.h> | |
19 | #include <linux/iio/iio.h> | |
20 | #include <linux/module.h> | |
21 | #include "inv_mpu_iio.h" | |
22 | ||
23 | static const struct regmap_config inv_mpu_regmap_config = { | |
24 | .reg_bits = 8, | |
25 | .val_bits = 8, | |
26 | }; | |
27 | ||
28 | /* | |
29 | * The i2c read/write needs to happen in unlocked mode. As the parent | |
30 | * adapter is common. If we use locked versions, it will fail as | |
31 | * the mux adapter will lock the parent i2c adapter, while calling | |
32 | * select/deselect functions. | |
33 | */ | |
34 | static int inv_mpu6050_write_reg_unlocked(struct i2c_client *client, | |
35 | u8 reg, u8 d) | |
36 | { | |
37 | int ret; | |
38 | u8 buf[2] = {reg, d}; | |
39 | struct i2c_msg msg[1] = { | |
40 | { | |
41 | .addr = client->addr, | |
42 | .flags = 0, | |
43 | .len = sizeof(buf), | |
44 | .buf = buf, | |
45 | } | |
46 | }; | |
47 | ||
48 | ret = __i2c_transfer(client->adapter, msg, 1); | |
49 | if (ret != 1) | |
50 | return ret; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv, | |
56 | u32 chan_id) | |
57 | { | |
58 | struct i2c_client *client = mux_priv; | |
59 | struct iio_dev *indio_dev = dev_get_drvdata(&client->dev); | |
60 | struct inv_mpu6050_state *st = iio_priv(indio_dev); | |
61 | int ret = 0; | |
62 | ||
63 | /* Use the same mutex which was used everywhere to protect power-op */ | |
64 | mutex_lock(&indio_dev->mlock); | |
65 | if (!st->powerup_count) { | |
66 | ret = inv_mpu6050_write_reg_unlocked(client, | |
67 | st->reg->pwr_mgmt_1, 0); | |
68 | if (ret) | |
69 | goto write_error; | |
70 | ||
8f356be3 MR |
71 | usleep_range(INV_MPU6050_REG_UP_TIME_MIN, |
72 | INV_MPU6050_REG_UP_TIME_MAX); | |
b3eea8da AR |
73 | } |
74 | if (!ret) { | |
75 | st->powerup_count++; | |
76 | ret = inv_mpu6050_write_reg_unlocked(client, | |
77 | st->reg->int_pin_cfg, | |
78 | INV_MPU6050_INT_PIN_CFG | | |
79 | INV_MPU6050_BIT_BYPASS_EN); | |
80 | } | |
81 | write_error: | |
82 | mutex_unlock(&indio_dev->mlock); | |
83 | ||
84 | return ret; | |
85 | } | |
86 | ||
87 | static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap, | |
88 | void *mux_priv, u32 chan_id) | |
89 | { | |
90 | struct i2c_client *client = mux_priv; | |
91 | struct iio_dev *indio_dev = dev_get_drvdata(&client->dev); | |
92 | struct inv_mpu6050_state *st = iio_priv(indio_dev); | |
93 | ||
94 | mutex_lock(&indio_dev->mlock); | |
95 | /* It doesn't really mattter, if any of the calls fails */ | |
96 | inv_mpu6050_write_reg_unlocked(client, st->reg->int_pin_cfg, | |
97 | INV_MPU6050_INT_PIN_CFG); | |
98 | st->powerup_count--; | |
99 | if (!st->powerup_count) | |
100 | inv_mpu6050_write_reg_unlocked(client, st->reg->pwr_mgmt_1, | |
101 | INV_MPU6050_BIT_SLEEP); | |
102 | mutex_unlock(&indio_dev->mlock); | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
393dbe4e DB |
107 | static const char *inv_mpu_match_acpi_device(struct device *dev, int *chip_id) |
108 | { | |
109 | const struct acpi_device_id *id; | |
110 | ||
111 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | |
112 | if (!id) | |
113 | return NULL; | |
114 | ||
115 | *chip_id = (int)id->driver_data; | |
116 | ||
117 | return dev_name(dev); | |
118 | } | |
119 | ||
b3eea8da AR |
120 | /** |
121 | * inv_mpu_probe() - probe function. | |
122 | * @client: i2c client. | |
123 | * @id: i2c device id. | |
124 | * | |
125 | * Returns 0 on success, a negative error code otherwise. | |
126 | */ | |
127 | static int inv_mpu_probe(struct i2c_client *client, | |
fc0dccdd | 128 | const struct i2c_device_id *id) |
b3eea8da AR |
129 | { |
130 | struct inv_mpu6050_state *st; | |
393dbe4e | 131 | int result, chip_type; |
b3eea8da | 132 | struct regmap *regmap; |
393dbe4e | 133 | const char *name; |
b3eea8da AR |
134 | |
135 | if (!i2c_check_functionality(client->adapter, | |
136 | I2C_FUNC_SMBUS_I2C_BLOCK)) | |
f8d9d3b4 | 137 | return -EOPNOTSUPP; |
b3eea8da | 138 | |
393dbe4e DB |
139 | if (id) { |
140 | chip_type = (int)id->driver_data; | |
141 | name = id->name; | |
142 | } else if (ACPI_HANDLE(&client->dev)) { | |
143 | name = inv_mpu_match_acpi_device(&client->dev, &chip_type); | |
144 | if (!name) | |
145 | return -ENODEV; | |
146 | } else { | |
147 | return -ENOSYS; | |
148 | } | |
149 | ||
b3eea8da AR |
150 | regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config); |
151 | if (IS_ERR(regmap)) { | |
152 | dev_err(&client->dev, "Failed to register i2c regmap %d\n", | |
153 | (int)PTR_ERR(regmap)); | |
154 | return PTR_ERR(regmap); | |
155 | } | |
156 | ||
33da559f | 157 | result = inv_mpu_core_probe(regmap, client->irq, name, |
140afdd9 | 158 | NULL, chip_type); |
b3eea8da AR |
159 | if (result < 0) |
160 | return result; | |
161 | ||
162 | st = iio_priv(dev_get_drvdata(&client->dev)); | |
163 | st->mux_adapter = i2c_add_mux_adapter(client->adapter, | |
164 | &client->dev, | |
165 | client, | |
166 | 0, 0, 0, | |
167 | inv_mpu6050_select_bypass, | |
168 | inv_mpu6050_deselect_bypass); | |
169 | if (!st->mux_adapter) { | |
170 | result = -ENODEV; | |
171 | goto out_unreg_device; | |
172 | } | |
173 | ||
174 | result = inv_mpu_acpi_create_mux_client(client); | |
175 | if (result) | |
176 | goto out_del_mux; | |
177 | ||
178 | return 0; | |
179 | ||
180 | out_del_mux: | |
181 | i2c_del_mux_adapter(st->mux_adapter); | |
182 | out_unreg_device: | |
183 | inv_mpu_core_remove(&client->dev); | |
184 | return result; | |
185 | } | |
186 | ||
187 | static int inv_mpu_remove(struct i2c_client *client) | |
188 | { | |
189 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
190 | struct inv_mpu6050_state *st = iio_priv(indio_dev); | |
191 | ||
192 | inv_mpu_acpi_delete_mux_client(client); | |
193 | i2c_del_mux_adapter(st->mux_adapter); | |
194 | ||
195 | return inv_mpu_core_remove(&client->dev); | |
196 | } | |
197 | ||
198 | /* | |
199 | * device id table is used to identify what device can be | |
200 | * supported by this driver | |
201 | */ | |
202 | static const struct i2c_device_id inv_mpu_id[] = { | |
203 | {"mpu6050", INV_MPU6050}, | |
204 | {"mpu6500", INV_MPU6500}, | |
205 | {} | |
206 | }; | |
207 | ||
208 | MODULE_DEVICE_TABLE(i2c, inv_mpu_id); | |
209 | ||
210 | static const struct acpi_device_id inv_acpi_match[] = { | |
211 | {"INVN6500", 0}, | |
212 | { }, | |
213 | }; | |
214 | ||
215 | MODULE_DEVICE_TABLE(acpi, inv_acpi_match); | |
216 | ||
217 | static struct i2c_driver inv_mpu_driver = { | |
218 | .probe = inv_mpu_probe, | |
219 | .remove = inv_mpu_remove, | |
220 | .id_table = inv_mpu_id, | |
221 | .driver = { | |
222 | .acpi_match_table = ACPI_PTR(inv_acpi_match), | |
223 | .name = "inv-mpu6050-i2c", | |
224 | .pm = &inv_mpu_pmops, | |
225 | }, | |
226 | }; | |
227 | ||
228 | module_i2c_driver(inv_mpu_driver); | |
229 | ||
230 | MODULE_AUTHOR("Invensense Corporation"); | |
231 | MODULE_DESCRIPTION("Invensense device MPU6050 driver"); | |
232 | MODULE_LICENSE("GPL"); |