Linux-libre 4.15.7-gnu
[librecmc/linux-libre.git] / drivers / iio / imu / inv_mpu6050 / inv_mpu_i2c.c
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/iio/iio.h>
19 #include <linux/module.h>
20 #include <linux/of_device.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 static 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 = 0;
33
34         /* Use the same mutex which was used everywhere to protect power-op */
35         mutex_lock(&st->lock);
36         if (!st->powerup_count) {
37                 ret = regmap_write(st->map, st->reg->pwr_mgmt_1, 0);
38                 if (ret)
39                         goto write_error;
40
41                 usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
42                              INV_MPU6050_REG_UP_TIME_MAX);
43         }
44         if (!ret) {
45                 st->powerup_count++;
46                 ret = regmap_write(st->map, st->reg->int_pin_cfg,
47                                    INV_MPU6050_INT_PIN_CFG |
48                                    INV_MPU6050_BIT_BYPASS_EN);
49         }
50 write_error:
51         mutex_unlock(&st->lock);
52
53         return ret;
54 }
55
56 static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id)
57 {
58         struct iio_dev *indio_dev = i2c_mux_priv(muxc);
59         struct inv_mpu6050_state *st = iio_priv(indio_dev);
60
61         mutex_lock(&st->lock);
62         /* It doesn't really mattter, if any of the calls fails */
63         regmap_write(st->map, st->reg->int_pin_cfg, INV_MPU6050_INT_PIN_CFG);
64         st->powerup_count--;
65         if (!st->powerup_count)
66                 regmap_write(st->map, st->reg->pwr_mgmt_1,
67                              INV_MPU6050_BIT_SLEEP);
68         mutex_unlock(&st->lock);
69
70         return 0;
71 }
72
73 static const char *inv_mpu_match_acpi_device(struct device *dev,
74                                              enum inv_devices *chip_id)
75 {
76         const struct acpi_device_id *id;
77
78         id = acpi_match_device(dev->driver->acpi_match_table, dev);
79         if (!id)
80                 return NULL;
81
82         *chip_id = (int)id->driver_data;
83
84         return dev_name(dev);
85 }
86
87 /**
88  *  inv_mpu_probe() - probe function.
89  *  @client:          i2c client.
90  *  @id:              i2c device id.
91  *
92  *  Returns 0 on success, a negative error code otherwise.
93  */
94 static int inv_mpu_probe(struct i2c_client *client,
95                          const struct i2c_device_id *id)
96 {
97         struct inv_mpu6050_state *st;
98         int result;
99         enum inv_devices chip_type;
100         struct regmap *regmap;
101         const char *name;
102
103         if (!i2c_check_functionality(client->adapter,
104                                      I2C_FUNC_SMBUS_I2C_BLOCK))
105                 return -EOPNOTSUPP;
106
107         if (client->dev.of_node) {
108                 chip_type = (enum inv_devices)
109                         of_device_get_match_data(&client->dev);
110                 name = client->name;
111         } else if (id) {
112                 chip_type = (enum inv_devices)
113                         id->driver_data;
114                 name = id->name;
115         } else if (ACPI_HANDLE(&client->dev)) {
116                 name = inv_mpu_match_acpi_device(&client->dev, &chip_type);
117                 if (!name)
118                         return -ENODEV;
119         } else {
120                 return -ENOSYS;
121         }
122
123         regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
124         if (IS_ERR(regmap)) {
125                 dev_err(&client->dev, "Failed to register i2c regmap %d\n",
126                         (int)PTR_ERR(regmap));
127                 return PTR_ERR(regmap);
128         }
129
130         result = inv_mpu_core_probe(regmap, client->irq, name,
131                                     NULL, chip_type);
132         if (result < 0)
133                 return result;
134
135         st = iio_priv(dev_get_drvdata(&client->dev));
136         st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
137                                  1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
138                                  inv_mpu6050_select_bypass,
139                                  inv_mpu6050_deselect_bypass);
140         if (!st->muxc) {
141                 result = -ENOMEM;
142                 goto out_unreg_device;
143         }
144         st->muxc->priv = dev_get_drvdata(&client->dev);
145         result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
146         if (result)
147                 goto out_unreg_device;
148
149         result = inv_mpu_acpi_create_mux_client(client);
150         if (result)
151                 goto out_del_mux;
152
153         return 0;
154
155 out_del_mux:
156         i2c_mux_del_adapters(st->muxc);
157 out_unreg_device:
158         inv_mpu_core_remove(&client->dev);
159         return result;
160 }
161
162 static int inv_mpu_remove(struct i2c_client *client)
163 {
164         struct iio_dev *indio_dev = i2c_get_clientdata(client);
165         struct inv_mpu6050_state *st = iio_priv(indio_dev);
166
167         inv_mpu_acpi_delete_mux_client(client);
168         i2c_mux_del_adapters(st->muxc);
169
170         return inv_mpu_core_remove(&client->dev);
171 }
172
173 /*
174  * device id table is used to identify what device can be
175  * supported by this driver
176  */
177 static const struct i2c_device_id inv_mpu_id[] = {
178         {"mpu6050", INV_MPU6050},
179         {"mpu6500", INV_MPU6500},
180         {"mpu9150", INV_MPU9150},
181         {"mpu9250", INV_MPU9250},
182         {"icm20608", INV_ICM20608},
183         {}
184 };
185
186 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
187
188 static const struct of_device_id inv_of_match[] = {
189         {
190                 .compatible = "invensense,mpu6050",
191                 .data = (void *)INV_MPU6050
192         },
193         {
194                 .compatible = "invensense,mpu6500",
195                 .data = (void *)INV_MPU6500
196         },
197         {
198                 .compatible = "invensense,mpu9150",
199                 .data = (void *)INV_MPU9150
200         },
201         {
202                 .compatible = "invensense,mpu9250",
203                 .data = (void *)INV_MPU9250
204         },
205         {
206                 .compatible = "invensense,icm20608",
207                 .data = (void *)INV_ICM20608
208         },
209         { }
210 };
211 MODULE_DEVICE_TABLE(of, inv_of_match);
212
213 static const struct acpi_device_id inv_acpi_match[] = {
214         {"INVN6500", INV_MPU6500},
215         { },
216 };
217
218 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
219
220 static struct i2c_driver inv_mpu_driver = {
221         .probe          =       inv_mpu_probe,
222         .remove         =       inv_mpu_remove,
223         .id_table       =       inv_mpu_id,
224         .driver = {
225                 .of_match_table = inv_of_match,
226                 .acpi_match_table = ACPI_PTR(inv_acpi_match),
227                 .name   =       "inv-mpu6050-i2c",
228                 .pm     =       &inv_mpu_pmops,
229         },
230 };
231
232 module_i2c_driver(inv_mpu_driver);
233
234 MODULE_AUTHOR("Invensense Corporation");
235 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
236 MODULE_LICENSE("GPL");