1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
5 
6 #include <linux/delay.h>
7 #include <linux/err.h>
8 #include <linux/i2c.h>
9 #include <linux/iio/iio.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 
14 #include "inv_mpu_iio.h"
15 
16 static const struct regmap_config inv_mpu_regmap_config = {
17 	.reg_bits = 8,
18 	.val_bits = 8,
19 };
20 
21 static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id)
22 {
23 	return 0;
24 }
25 
26 static bool inv_mpu_i2c_aux_bus(struct device *dev)
27 {
28 	struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
29 
30 	switch (st->chip_type) {
31 	case INV_ICM20608:
32 	case INV_ICM20608D:
33 	case INV_ICM20609:
34 	case INV_ICM20689:
35 	case INV_ICM20602:
36 	case INV_IAM20680:
37 		/* no i2c auxiliary bus on the chip */
38 		return false;
39 	case INV_MPU9150:
40 	case INV_MPU9250:
41 	case INV_MPU9255:
42 		if (st->magn_disabled)
43 			return true;
44 		else
45 			return false;
46 	default:
47 		return true;
48 	}
49 }
50 
51 static int inv_mpu_i2c_aux_setup(struct iio_dev *indio_dev)
52 {
53 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
54 	struct device *dev = indio_dev->dev.parent;
55 	struct fwnode_handle *mux_node;
56 	int ret;
57 
58 	/*
59 	 * MPU9xxx magnetometer support requires to disable i2c auxiliary bus.
60 	 * To ensure backward compatibility with existing setups, do not disable
61 	 * i2c auxiliary bus if it used.
62 	 * Check for i2c-gate node in devicetree and set magnetometer disabled.
63 	 * Only MPU6500 is supported by ACPI, no need to check.
64 	 */
65 	switch (st->chip_type) {
66 	case INV_MPU9150:
67 	case INV_MPU9250:
68 	case INV_MPU9255:
69 		mux_node = device_get_named_child_node(dev, "i2c-gate");
70 		if (mux_node != NULL) {
71 			st->magn_disabled = true;
72 			dev_warn(dev, "disable internal use of magnetometer\n");
73 		}
74 		fwnode_handle_put(mux_node);
75 		break;
76 	default:
77 		break;
78 	}
79 
80 	/* enable i2c bypass when using i2c auxiliary bus */
81 	if (inv_mpu_i2c_aux_bus(dev)) {
82 		ret = regmap_write(st->map, st->reg->int_pin_cfg,
83 				   st->irq_mask | INV_MPU6050_BIT_BYPASS_EN);
84 		if (ret)
85 			return ret;
86 	}
87 
88 	return 0;
89 }
90 
91 /**
92  *  inv_mpu_probe() - probe function.
93  *  @client:          i2c client.
94  *
95  *  Returns 0 on success, a negative error code otherwise.
96  */
97 static int inv_mpu_probe(struct i2c_client *client)
98 {
99 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
100 	const void *match;
101 	struct inv_mpu6050_state *st;
102 	int result;
103 	enum inv_devices chip_type;
104 	struct regmap *regmap;
105 	const char *name;
106 
107 	if (!i2c_check_functionality(client->adapter,
108 				     I2C_FUNC_SMBUS_I2C_BLOCK))
109 		return -EOPNOTSUPP;
110 
111 	match = device_get_match_data(&client->dev);
112 	if (match) {
113 		chip_type = (uintptr_t)match;
114 		name = client->name;
115 	} else if (id) {
116 		chip_type = (enum inv_devices)
117 			id->driver_data;
118 		name = id->name;
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: %pe\n",
126 			regmap);
127 		return PTR_ERR(regmap);
128 	}
129 
130 	result = inv_mpu_core_probe(regmap, client->irq, name,
131 				    inv_mpu_i2c_aux_setup, chip_type);
132 	if (result < 0)
133 		return result;
134 
135 	st = iio_priv(dev_get_drvdata(&client->dev));
136 	if (inv_mpu_i2c_aux_bus(&client->dev)) {
137 		/* declare i2c auxiliary bus */
138 		st->muxc = i2c_mux_alloc(client->adapter, &client->dev,
139 					 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE,
140 					 inv_mpu6050_select_bypass, NULL);
141 		if (!st->muxc)
142 			return -ENOMEM;
143 		st->muxc->priv = dev_get_drvdata(&client->dev);
144 		result = i2c_mux_add_adapter(st->muxc, 0, 0, 0);
145 		if (result)
146 			return result;
147 		result = inv_mpu_acpi_create_mux_client(client);
148 		if (result)
149 			goto out_del_mux;
150 	}
151 
152 	return 0;
153 
154 out_del_mux:
155 	i2c_mux_del_adapters(st->muxc);
156 	return result;
157 }
158 
159 static void inv_mpu_remove(struct i2c_client *client)
160 {
161 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
162 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
163 
164 	if (st->muxc) {
165 		inv_mpu_acpi_delete_mux_client(client);
166 		i2c_mux_del_adapters(st->muxc);
167 	}
168 }
169 
170 /*
171  * device id table is used to identify what device can be
172  * supported by this driver
173  */
174 static const struct i2c_device_id inv_mpu_id[] = {
175 	{"mpu6050", INV_MPU6050},
176 	{"mpu6500", INV_MPU6500},
177 	{"mpu6515", INV_MPU6515},
178 	{"mpu6880", INV_MPU6880},
179 	{"mpu9150", INV_MPU9150},
180 	{"mpu9250", INV_MPU9250},
181 	{"mpu9255", INV_MPU9255},
182 	{"icm20608", INV_ICM20608},
183 	{"icm20608d", INV_ICM20608D},
184 	{"icm20609", INV_ICM20609},
185 	{"icm20689", INV_ICM20689},
186 	{"icm20602", INV_ICM20602},
187 	{"icm20690", INV_ICM20690},
188 	{"iam20680", INV_IAM20680},
189 	{}
190 };
191 
192 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
193 
194 static const struct of_device_id inv_of_match[] = {
195 	{
196 		.compatible = "invensense,mpu6050",
197 		.data = (void *)INV_MPU6050
198 	},
199 	{
200 		.compatible = "invensense,mpu6500",
201 		.data = (void *)INV_MPU6500
202 	},
203 	{
204 		.compatible = "invensense,mpu6515",
205 		.data = (void *)INV_MPU6515
206 	},
207 	{
208 		.compatible = "invensense,mpu6880",
209 		.data = (void *)INV_MPU6880
210 	},
211 	{
212 		.compatible = "invensense,mpu9150",
213 		.data = (void *)INV_MPU9150
214 	},
215 	{
216 		.compatible = "invensense,mpu9250",
217 		.data = (void *)INV_MPU9250
218 	},
219 	{
220 		.compatible = "invensense,mpu9255",
221 		.data = (void *)INV_MPU9255
222 	},
223 	{
224 		.compatible = "invensense,icm20608",
225 		.data = (void *)INV_ICM20608
226 	},
227 	{
228 		.compatible = "invensense,icm20608d",
229 		.data = (void *)INV_ICM20608D
230 	},
231 	{
232 		.compatible = "invensense,icm20609",
233 		.data = (void *)INV_ICM20609
234 	},
235 	{
236 		.compatible = "invensense,icm20689",
237 		.data = (void *)INV_ICM20689
238 	},
239 	{
240 		.compatible = "invensense,icm20602",
241 		.data = (void *)INV_ICM20602
242 	},
243 	{
244 		.compatible = "invensense,icm20690",
245 		.data = (void *)INV_ICM20690
246 	},
247 	{
248 		.compatible = "invensense,iam20680",
249 		.data = (void *)INV_IAM20680
250 	},
251 	{ }
252 };
253 MODULE_DEVICE_TABLE(of, inv_of_match);
254 
255 static const struct acpi_device_id inv_acpi_match[] = {
256 	{"INVN6500", INV_MPU6500},
257 	{ },
258 };
259 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
260 
261 static struct i2c_driver inv_mpu_driver = {
262 	.probe_new	=	inv_mpu_probe,
263 	.remove		=	inv_mpu_remove,
264 	.id_table	=	inv_mpu_id,
265 	.driver = {
266 		.of_match_table = inv_of_match,
267 		.acpi_match_table = inv_acpi_match,
268 		.name	=	"inv-mpu6050-i2c",
269 		.pm     =       pm_ptr(&inv_mpu_pmops),
270 	},
271 };
272 
273 module_i2c_driver(inv_mpu_driver);
274 
275 MODULE_AUTHOR("Invensense Corporation");
276 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
277 MODULE_LICENSE("GPL");
278 MODULE_IMPORT_NS(IIO_MPU6050);
279