xref: /linux/drivers/iio/accel/bmc150-accel-i2c.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * 3-axis accelerometer driver supporting many I2C Bosch-Sensortec chips
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 
7 #include <linux/device.h>
8 #include <linux/i2c.h>
9 #include <linux/module.h>
10 #include <linux/acpi.h>
11 #include <linux/regmap.h>
12 
13 #include "bmc150-accel.h"
14 
15 #ifdef CONFIG_ACPI
16 static const struct acpi_device_id bmc150_acpi_dual_accel_ids[] = {
17 	{"BOSC0200"},
18 	{"DUAL250E"},
19 	{ }
20 };
21 
22 /*
23  * The DUAL250E ACPI device for 360° hinges type 2-in-1s with 1 accelerometer
24  * in the display and 1 in the hinge has an ACPI-method (DSM) to tell the
25  * ACPI code about the angle between the 2 halves. This will make the ACPI
26  * code enable/disable the keyboard and touchpad. We need to call this to avoid
27  * the keyboard being disabled when the 2-in-1 is turned-on or resumed while
28  * fully folded into tablet mode (which gets detected with a HALL-sensor).
29  * If we don't call this then the keyboard won't work even when the 2-in-1 is
30  * changed to be used in laptop mode after the power-on / resume.
31  *
32  * This DSM takes 2 angles, selected by setting aux0 to 0 or 1, these presumably
33  * define the angle between the gravity vector measured by the accelerometer in
34  * the display (aux0=0) resp. the base (aux0=1) and some reference vector.
35  * The 2 angles get subtracted from each other so the reference vector does
36  * not matter and we can simply leave the second angle at 0.
37  */
38 
39 #define BMC150_DSM_GUID				"7681541e-8827-4239-8d9d-36be7fe12542"
40 #define DUAL250E_SET_ANGLE_FN_INDEX		3
41 
42 struct dual250e_set_angle_args {
43 	u32 aux0;
44 	u32 ang0;
45 	u32 rawx;
46 	u32 rawy;
47 	u32 rawz;
48 } __packed;
49 
bmc150_acpi_set_angle_dsm(struct i2c_client * client,u32 aux0,u32 ang0)50 static bool bmc150_acpi_set_angle_dsm(struct i2c_client *client, u32 aux0, u32 ang0)
51 {
52 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
53 	struct dual250e_set_angle_args args = {
54 		.aux0 = aux0,
55 		.ang0 = ang0,
56 	};
57 	union acpi_object args_obj, *obj;
58 	guid_t guid;
59 
60 	if (!acpi_dev_hid_uid_match(adev, "DUAL250E", NULL))
61 		return false;
62 
63 	guid_parse(BMC150_DSM_GUID, &guid);
64 
65 	if (!acpi_check_dsm(adev->handle, &guid, 0, BIT(DUAL250E_SET_ANGLE_FN_INDEX)))
66 		return false;
67 
68 	/*
69 	 * Note this triggers the following warning:
70 	 * "ACPI Warning: \_SB.PCI0.I2C2.ACC1._DSM: Argument #4 type mismatch -
71 	 *                Found [Buffer], ACPI requires [Package]"
72 	 * This is unavoidable since the _DSM implementation expects a "naked"
73 	 * buffer, so wrapping it in a package will _not_ work.
74 	 */
75 	args_obj.type = ACPI_TYPE_BUFFER;
76 	args_obj.buffer.length = sizeof(args);
77 	args_obj.buffer.pointer = (u8 *)&args;
78 
79 	obj = acpi_evaluate_dsm(adev->handle, &guid, 0, DUAL250E_SET_ANGLE_FN_INDEX, &args_obj);
80 	if (!obj) {
81 		dev_err(&client->dev, "Failed to call DSM to enable keyboard and touchpad\n");
82 		return false;
83 	}
84 
85 	ACPI_FREE(obj);
86 	return true;
87 }
88 
bmc150_acpi_enable_keyboard(struct i2c_client * client)89 static bool bmc150_acpi_enable_keyboard(struct i2c_client *client)
90 {
91 	/*
92 	 * The EC must see a change for it to re-enable the kbd, so first
93 	 * set the angle to 270° (tent/stand mode) and then change it to
94 	 * 90° (laptop mode).
95 	 */
96 	if (!bmc150_acpi_set_angle_dsm(client, 0, 270))
97 		return false;
98 
99 	/* The EC needs some time to notice the angle being changed */
100 	msleep(100);
101 
102 	return bmc150_acpi_set_angle_dsm(client, 0, 90);
103 }
104 
bmc150_acpi_resume_work(struct work_struct * work)105 static void bmc150_acpi_resume_work(struct work_struct *work)
106 {
107 	struct bmc150_accel_data *data =
108 		container_of(work, struct bmc150_accel_data, resume_work.work);
109 
110 	bmc150_acpi_enable_keyboard(data->second_device);
111 }
112 
bmc150_acpi_resume_handler(struct device * dev)113 static void bmc150_acpi_resume_handler(struct device *dev)
114 {
115 	struct bmc150_accel_data *data = iio_priv(dev_get_drvdata(dev));
116 
117 	/*
118 	 * Delay the bmc150_acpi_enable_keyboard() call till after the system
119 	 * resume has completed, otherwise it will not work.
120 	 */
121 	schedule_delayed_work(&data->resume_work, msecs_to_jiffies(1000));
122 }
123 
124 /*
125  * Some acpi_devices describe 2 accelerometers in a single ACPI device,
126  * try instantiating a second i2c_client for an I2cSerialBusV2 ACPI resource
127  * with index 1.
128  */
bmc150_acpi_dual_accel_probe(struct i2c_client * client)129 static void bmc150_acpi_dual_accel_probe(struct i2c_client *client)
130 {
131 	struct bmc150_accel_data *data = iio_priv(i2c_get_clientdata(client));
132 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
133 	char dev_name[16];
134 	struct i2c_board_info board_info = {
135 		.type = "bmc150_accel",
136 		.dev_name = dev_name,
137 		.fwnode = client->dev.fwnode,
138 	};
139 
140 	if (acpi_match_device_ids(adev, bmc150_acpi_dual_accel_ids))
141 		return;
142 
143 	/*
144 	 * The 2nd accel sits in the base of 2-in-1s. The suffix is static, as
145 	 * there should never be more then 1 ACPI node with 2 accelerometers.
146 	 */
147 	snprintf(dev_name, sizeof(dev_name), "%s:base", acpi_device_hid(adev));
148 
149 	board_info.irq = acpi_dev_gpio_irq_get(adev, 1);
150 
151 	data->second_device = i2c_acpi_new_device(&client->dev, 1, &board_info);
152 
153 	if (!IS_ERR(data->second_device) && bmc150_acpi_enable_keyboard(data->second_device)) {
154 		INIT_DELAYED_WORK(&data->resume_work, bmc150_acpi_resume_work);
155 		data->resume_callback = bmc150_acpi_resume_handler;
156 	}
157 }
158 
bmc150_acpi_dual_accel_remove(struct i2c_client * client)159 static void bmc150_acpi_dual_accel_remove(struct i2c_client *client)
160 {
161 	struct bmc150_accel_data *data = iio_priv(i2c_get_clientdata(client));
162 
163 	if (data->resume_callback)
164 		cancel_delayed_work_sync(&data->resume_work);
165 
166 	i2c_unregister_device(data->second_device);
167 }
168 #else
bmc150_acpi_dual_accel_probe(struct i2c_client * client)169 static void bmc150_acpi_dual_accel_probe(struct i2c_client *client) {}
bmc150_acpi_dual_accel_remove(struct i2c_client * client)170 static void bmc150_acpi_dual_accel_remove(struct i2c_client *client) {}
171 #endif
172 
bmc150_accel_probe(struct i2c_client * client)173 static int bmc150_accel_probe(struct i2c_client *client)
174 {
175 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
176 	struct regmap *regmap;
177 	const char *name = NULL;
178 	enum bmc150_type type = BOSCH_UNKNOWN;
179 	bool block_supported =
180 		i2c_check_functionality(client->adapter, I2C_FUNC_I2C) ||
181 		i2c_check_functionality(client->adapter,
182 					I2C_FUNC_SMBUS_READ_I2C_BLOCK);
183 	int ret;
184 
185 	regmap = devm_regmap_init_i2c(client, &bmc150_regmap_conf);
186 	if (IS_ERR(regmap)) {
187 		dev_err(&client->dev, "Failed to initialize i2c regmap\n");
188 		return PTR_ERR(regmap);
189 	}
190 
191 	if (id) {
192 		name = id->name;
193 		type = id->driver_data;
194 	}
195 
196 	ret = bmc150_accel_core_probe(&client->dev, regmap, client->irq,
197 				      type, name, block_supported);
198 	if (ret)
199 		return ret;
200 
201 	/*
202 	 * The !id check avoids recursion when probe() gets called
203 	 * for the second client.
204 	 */
205 	if (!id && has_acpi_companion(&client->dev))
206 		bmc150_acpi_dual_accel_probe(client);
207 
208 	return 0;
209 }
210 
bmc150_accel_remove(struct i2c_client * client)211 static void bmc150_accel_remove(struct i2c_client *client)
212 {
213 	bmc150_acpi_dual_accel_remove(client);
214 
215 	bmc150_accel_core_remove(&client->dev);
216 }
217 
218 static const struct acpi_device_id bmc150_accel_acpi_match[] = {
219 	{"BMA0255"},
220 	{"BMA0280"},
221 	{"BMA222"},
222 	{"BMA222E"},
223 	{"BMA250E"},
224 	{"BMC150A"},
225 	{"BMI055A"},
226 	/*
227 	 * The "BOSC0200" identifier used here is not unique to devices using
228 	 * bmc150. The same "BOSC0200" identifier is found in the ACPI tables
229 	 * of the ASUS ROG ALLY and Ayaneo AIR Plus which both use a Bosch
230 	 * BMI323 chip. This creates a conflict with duplicate ACPI identifiers
231 	 * which multiple drivers want to use. Fortunately, when the bmc150
232 	 * driver starts to load on the ASUS ROG ALLY, the chip ID check
233 	 * portion fails (correctly) because the chip IDs received (via i2c)
234 	 * are unique between bmc150 and bmi323 and a dmesg output similar to
235 	 * this: "bmc150_accel_i2c i2c-BOSC0200:00: Invalid chip 0" can be
236 	 * seen. This allows the bmi323 driver to take over for ASUS ROG ALLY,
237 	 * and other devices using the bmi323 chip.
238 	 */
239 	{"BOSC0200"},
240 	{"BSBA0150"},
241 	{"DUAL250E"},
242 	{ }
243 };
244 MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match);
245 
246 static const struct i2c_device_id bmc150_accel_id[] = {
247 	{ .name = "bma222", .driver_data = BOSCH_UNKNOWN },
248 	{ .name = "bma222e", .driver_data = BOSCH_UNKNOWN },
249 	{ .name = "bma250e", .driver_data = BOSCH_UNKNOWN },
250 	{ .name = "bma253", .driver_data = BOSCH_UNKNOWN },
251 	{ .name = "bma254", .driver_data = BOSCH_UNKNOWN },
252 	{ .name = "bma255", .driver_data = BOSCH_UNKNOWN },
253 	{ .name = "bma280", .driver_data = BOSCH_UNKNOWN },
254 	{ .name = "bmc150_accel", .driver_data = BOSCH_UNKNOWN },
255 	{ .name = "bmc156_accel", .driver_data = BOSCH_BMC156 },
256 	{ .name = "bmi055_accel", .driver_data = BOSCH_UNKNOWN },
257 	{ }
258 };
259 
260 MODULE_DEVICE_TABLE(i2c, bmc150_accel_id);
261 
262 static const struct of_device_id bmc150_accel_of_match[] = {
263 	{ .compatible = "bosch,bma222" },
264 	{ .compatible = "bosch,bma222e" },
265 	{ .compatible = "bosch,bma250e" },
266 	{ .compatible = "bosch,bma253" },
267 	{ .compatible = "bosch,bma254" },
268 	{ .compatible = "bosch,bma255" },
269 	{ .compatible = "bosch,bma280" },
270 	{ .compatible = "bosch,bmc150_accel" },
271 	{ .compatible = "bosch,bmc156_accel" },
272 	{ .compatible = "bosch,bmi055_accel" },
273 	{ }
274 };
275 MODULE_DEVICE_TABLE(of, bmc150_accel_of_match);
276 
277 static struct i2c_driver bmc150_accel_driver = {
278 	.driver = {
279 		.name	= "bmc150_accel_i2c",
280 		.of_match_table = bmc150_accel_of_match,
281 		.acpi_match_table = bmc150_accel_acpi_match,
282 		.pm	= &bmc150_accel_pm_ops,
283 	},
284 	.probe		= bmc150_accel_probe,
285 	.remove		= bmc150_accel_remove,
286 	.id_table	= bmc150_accel_id,
287 };
288 module_i2c_driver(bmc150_accel_driver);
289 
290 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
291 MODULE_LICENSE("GPL v2");
292 MODULE_DESCRIPTION("BMC150 I2C accelerometer driver");
293 MODULE_IMPORT_NS("IIO_BMC150");
294