treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / imu / inv_mpu6050 / inv_mpu_i2c.c
blobf47a28b4be23e117f3113e92a0618377cf2cba05
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
6 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/i2c.h>
10 #include <linux/iio/iio.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include "inv_mpu_iio.h"
15 static const struct regmap_config inv_mpu_regmap_config = {
16 .reg_bits = 8,
17 .val_bits = 8,
20 static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id)
22 struct iio_dev *indio_dev = i2c_mux_priv(muxc);
23 struct inv_mpu6050_state *st = iio_priv(indio_dev);
24 int ret;
26 mutex_lock(&st->lock);
28 ret = inv_mpu6050_set_power_itg(st, true);
29 if (ret)
30 goto error_unlock;
32 ret = regmap_write(st->map, st->reg->int_pin_cfg,
33 st->irq_mask | INV_MPU6050_BIT_BYPASS_EN);
35 error_unlock:
36 mutex_unlock(&st->lock);
38 return ret;
41 static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id)
43 struct iio_dev *indio_dev = i2c_mux_priv(muxc);
44 struct inv_mpu6050_state *st = iio_priv(indio_dev);
46 mutex_lock(&st->lock);
48 /* It doesn't really matter if any of the calls fail */
49 regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
50 inv_mpu6050_set_power_itg(st, false);
52 mutex_unlock(&st->lock);
54 return 0;
57 static const char *inv_mpu_match_acpi_device(struct device *dev,
58 enum inv_devices *chip_id)
60 const struct acpi_device_id *id;
62 id = acpi_match_device(dev->driver->acpi_match_table, dev);
63 if (!id)
64 return NULL;
66 *chip_id = (int)id->driver_data;
68 return dev_name(dev);
71 static bool inv_mpu_i2c_aux_bus(struct device *dev)
73 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
75 switch (st->chip_type) {
76 case INV_ICM20608:
77 case INV_ICM20602:
78 /* no i2c auxiliary bus on the chip */
79 return false;
80 case INV_MPU9150:
81 case INV_MPU9250:
82 case INV_MPU9255:
83 if (st->magn_disabled)
84 return true;
85 else
86 return false;
87 default:
88 return true;
93 * MPU9xxx magnetometer support requires to disable i2c auxiliary bus support.
94 * To ensure backward compatibility with existing setups, do not disable
95 * i2c auxiliary bus if it used.
96 * Check for i2c-gate node in devicetree and set magnetometer disabled.
97 * Only MPU6500 is supported by ACPI, no need to check.
99 static int inv_mpu_magn_disable(struct iio_dev *indio_dev)
101 struct inv_mpu6050_state *st = iio_priv(indio_dev);
102 struct device *dev = indio_dev->dev.parent;
103 struct device_node *mux_node;
105 switch (st->chip_type) {
106 case INV_MPU9150:
107 case INV_MPU9250:
108 case INV_MPU9255:
109 mux_node = of_get_child_by_name(dev->of_node, "i2c-gate");
110 if (mux_node != NULL) {
111 st->magn_disabled = true;
112 dev_warn(dev, "disable internal use of magnetometer\n");
114 of_node_put(mux_node);
115 break;
116 default:
117 break;
120 return 0;
124 * inv_mpu_probe() - probe function.
125 * @client: i2c client.
126 * @id: i2c device id.
128 * Returns 0 on success, a negative error code otherwise.
130 static int inv_mpu_probe(struct i2c_client *client,
131 const struct i2c_device_id *id)
133 struct inv_mpu6050_state *st;
134 int result;
135 enum inv_devices chip_type;
136 struct regmap *regmap;
137 const char *name;
139 if (!i2c_check_functionality(client->adapter,
140 I2C_FUNC_SMBUS_I2C_BLOCK))
141 return -EOPNOTSUPP;
143 if (client->dev.of_node) {
144 chip_type = (enum inv_devices)
145 of_device_get_match_data(&client->dev);
146 name = client->name;
147 } else if (id) {
148 chip_type = (enum inv_devices)
149 id->driver_data;
150 name = id->name;
151 } else if (ACPI_HANDLE(&client->dev)) {
152 name = inv_mpu_match_acpi_device(&client->dev, &chip_type);
153 if (!name)
154 return -ENODEV;
155 } else {
156 return -ENOSYS;
159 regmap = devm_regmap_init_i2c(client, &inv_mpu_regmap_config);
160 if (IS_ERR(regmap)) {
161 dev_err(&client->dev, "Failed to register i2c regmap %d\n",
162 (int)PTR_ERR(regmap));
163 return PTR_ERR(regmap);
166 result = inv_mpu_core_probe(regmap, client->irq, name,
167 inv_mpu_magn_disable, chip_type);
168 if (result < 0)
169 return result;
171 st = iio_priv(dev_get_drvdata(&client->dev));
172 if (inv_mpu_i2c_aux_bus(&client->dev)) {
173 /* declare i2c auxiliary bus */
174 st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
175 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
176 inv_mpu6050_select_bypass,
177 inv_mpu6050_deselect_bypass);
178 if (!st->muxc)
179 return -ENOMEM;
180 st->muxc->priv = dev_get_drvdata(&client->dev);
181 result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
182 if (result)
183 return result;
184 result = inv_mpu_acpi_create_mux_client(client);
185 if (result)
186 goto out_del_mux;
189 return 0;
191 out_del_mux:
192 i2c_mux_del_adapters(st->muxc);
193 return result;
196 static int inv_mpu_remove(struct i2c_client *client)
198 struct iio_dev *indio_dev = i2c_get_clientdata(client);
199 struct inv_mpu6050_state *st = iio_priv(indio_dev);
201 if (st->muxc) {
202 inv_mpu_acpi_delete_mux_client(client);
203 i2c_mux_del_adapters(st->muxc);
206 return 0;
210 * device id table is used to identify what device can be
211 * supported by this driver
213 static const struct i2c_device_id inv_mpu_id[] = {
214 {"mpu6050", INV_MPU6050},
215 {"mpu6500", INV_MPU6500},
216 {"mpu6515", INV_MPU6515},
217 {"mpu9150", INV_MPU9150},
218 {"mpu9250", INV_MPU9250},
219 {"mpu9255", INV_MPU9255},
220 {"icm20608", INV_ICM20608},
221 {"icm20602", INV_ICM20602},
225 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
227 static const struct of_device_id inv_of_match[] = {
229 .compatible = "invensense,mpu6050",
230 .data = (void *)INV_MPU6050
233 .compatible = "invensense,mpu6500",
234 .data = (void *)INV_MPU6500
237 .compatible = "invensense,mpu6515",
238 .data = (void *)INV_MPU6515
241 .compatible = "invensense,mpu9150",
242 .data = (void *)INV_MPU9150
245 .compatible = "invensense,mpu9250",
246 .data = (void *)INV_MPU9250
249 .compatible = "invensense,mpu9255",
250 .data = (void *)INV_MPU9255
253 .compatible = "invensense,icm20608",
254 .data = (void *)INV_ICM20608
257 .compatible = "invensense,icm20602",
258 .data = (void *)INV_ICM20602
262 MODULE_DEVICE_TABLE(of, inv_of_match);
264 static const struct acpi_device_id inv_acpi_match[] = {
265 {"INVN6500", INV_MPU6500},
266 { },
269 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
271 static struct i2c_driver inv_mpu_driver = {
272 .probe = inv_mpu_probe,
273 .remove = inv_mpu_remove,
274 .id_table = inv_mpu_id,
275 .driver = {
276 .of_match_table = inv_of_match,
277 .acpi_match_table = ACPI_PTR(inv_acpi_match),
278 .name = "inv-mpu6050-i2c",
279 .pm = &inv_mpu_pmops,
283 module_i2c_driver(inv_mpu_driver);
285 MODULE_AUTHOR("Invensense Corporation");
286 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
287 MODULE_LICENSE("GPL");