xref: /linux/drivers/platform/x86/dell/dell-lis3lv02d.c (revision 205a7309cccd34ad49c2b6b1b59b907c12395d6c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lis3lv02d i2c-client instantiation for ACPI SMO88xx devices without I2C resources.
4  *
5  *  Copyright (C) 2024 Hans de Goede <hansg@kernel.org>
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/device/bus.h>
10 #include <linux/dmi.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/notifier.h>
14 #include <linux/platform_device.h>
15 #include <linux/workqueue.h>
16 #include "dell-smo8800-ids.h"
17 
18 #define LIS3_WHO_AM_I 0x0f
19 
20 #define DELL_LIS3LV02D_DMI_ENTRY(product_name, i2c_addr)                 \
21 	{                                                                \
22 		.matches = {                                             \
23 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),    \
24 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, product_name), \
25 		},                                                       \
26 		.driver_data = (void *)(uintptr_t)(i2c_addr),            \
27 	}
28 
29 /*
30  * Accelerometer's I2C address is not specified in DMI nor ACPI,
31  * so it is needed to define mapping table based on DMI product names.
32  */
33 static const struct dmi_system_id lis3lv02d_devices[] __initconst = {
34 	/*
35 	 * Dell platform team told us that these Latitude devices have
36 	 * ST microelectronics accelerometer at I2C address 0x29.
37 	 */
38 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E5250",     0x29),
39 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E5450",     0x29),
40 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E5550",     0x29),
41 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E6440",     0x29),
42 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E6440 ATG", 0x29),
43 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E6540",     0x29),
44 	/*
45 	 * Additional individual entries were added after verification.
46 	 */
47 	DELL_LIS3LV02D_DMI_ENTRY("Latitude 5480",      0x29),
48 	DELL_LIS3LV02D_DMI_ENTRY("Latitude 5500",      0x29),
49 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E6330",     0x29),
50 	DELL_LIS3LV02D_DMI_ENTRY("Latitude E6430",     0x29),
51 	DELL_LIS3LV02D_DMI_ENTRY("Precision 3540",     0x29),
52 	DELL_LIS3LV02D_DMI_ENTRY("Precision 3551",     0x29),
53 	DELL_LIS3LV02D_DMI_ENTRY("Precision M6800",    0x29),
54 	DELL_LIS3LV02D_DMI_ENTRY("Vostro V131",        0x1d),
55 	DELL_LIS3LV02D_DMI_ENTRY("Vostro 5568",        0x29),
56 	DELL_LIS3LV02D_DMI_ENTRY("XPS 15 7590",        0x29),
57 	DELL_LIS3LV02D_DMI_ENTRY("XPS 15 9550",        0x29),
58 	{ }
59 };
60 
61 static u8 i2c_addr;
62 static struct i2c_client *i2c_dev;
63 static bool notifier_registered;
64 
65 static bool probe_i2c_addr;
66 module_param(probe_i2c_addr, bool, 0444);
67 MODULE_PARM_DESC(probe_i2c_addr, "Probe the i801 I2C bus for the accelerometer on models where the address is unknown, this may be dangerous.");
68 
69 static int detect_lis3lv02d(struct i2c_adapter *adap, unsigned short addr)
70 {
71 	union i2c_smbus_data smbus_data;
72 	int err;
73 
74 	dev_info(&adap->dev, "Probing for lis3lv02d on address 0x%02x\n", addr);
75 
76 	err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, LIS3_WHO_AM_I,
77 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
78 	if (err < 0)
79 		return 0; /* Not found */
80 
81 	/* valid who-am-i values are from drivers/misc/lis3lv02d/lis3lv02d.c */
82 	switch (smbus_data.byte) {
83 	case 0x32:
84 	case 0x33:
85 	case 0x3a:
86 	case 0x3b:
87 		break;
88 	default:
89 		dev_warn(&adap->dev, "Unknown who-am-i register value 0x%02x\n",
90 			 smbus_data.byte);
91 		return 0; /* Not found */
92 	}
93 
94 	dev_info(&adap->dev,
95 		 "Detected lis3lv02d on address 0x%02x, please report this upstream to platform-driver-x86@vger.kernel.org so that a quirk can be added\n",
96 		 addr);
97 
98 	return 1; /* Found */
99 }
100 
101 static bool i2c_adapter_is_main_i801(struct i2c_adapter *adap)
102 {
103 	/*
104 	 * Only match the main I801 adapter and reject secondary adapters
105 	 * which names start with "SMBus I801 IDF adapter".
106 	 */
107 	return strstarts(adap->name, "SMBus I801 adapter");
108 }
109 
110 static int find_i801(struct device *dev, void *data)
111 {
112 	struct i2c_adapter *adap, **adap_ret = data;
113 
114 	adap = i2c_verify_adapter(dev);
115 	if (!adap)
116 		return 0;
117 
118 	if (!i2c_adapter_is_main_i801(adap))
119 		return 0;
120 
121 	*adap_ret = i2c_get_adapter(adap->nr);
122 	return 1;
123 }
124 
125 static void instantiate_i2c_client(struct work_struct *work)
126 {
127 	struct i2c_board_info info = { };
128 	struct i2c_adapter *adap = NULL;
129 
130 	if (i2c_dev)
131 		return;
132 
133 	/*
134 	 * bus_for_each_dev() and not i2c_for_each_dev() to avoid
135 	 * a deadlock when find_i801() calls i2c_get_adapter().
136 	 */
137 	bus_for_each_dev(&i2c_bus_type, NULL, &adap, find_i801);
138 	if (!adap)
139 		return;
140 
141 	strscpy(info.type, "lis3lv02d", I2C_NAME_SIZE);
142 
143 	if (i2c_addr) {
144 		info.addr = i2c_addr;
145 		i2c_dev = i2c_new_client_device(adap, &info);
146 	} else {
147 		/* First try address 0x29 (most used) and then try 0x1d */
148 		static const unsigned short addr_list[] = { 0x29, 0x1d, I2C_CLIENT_END };
149 
150 		i2c_dev = i2c_new_scanned_device(adap, &info, addr_list, detect_lis3lv02d);
151 	}
152 
153 	if (IS_ERR(i2c_dev)) {
154 		dev_err(&adap->dev, "error %ld registering i2c_client\n", PTR_ERR(i2c_dev));
155 		i2c_dev = NULL;
156 	} else {
157 		dev_dbg(&adap->dev, "registered lis3lv02d on address 0x%02x\n", info.addr);
158 	}
159 
160 	i2c_put_adapter(adap);
161 }
162 static DECLARE_WORK(i2c_work, instantiate_i2c_client);
163 
164 static int i2c_bus_notify(struct notifier_block *nb, unsigned long action, void *data)
165 {
166 	struct device *dev = data;
167 	struct i2c_client *client;
168 	struct i2c_adapter *adap;
169 
170 	switch (action) {
171 	case BUS_NOTIFY_ADD_DEVICE:
172 		adap = i2c_verify_adapter(dev);
173 		if (!adap)
174 			break;
175 
176 		if (i2c_adapter_is_main_i801(adap))
177 			queue_work(system_long_wq, &i2c_work);
178 		break;
179 	case BUS_NOTIFY_REMOVED_DEVICE:
180 		client = i2c_verify_client(dev);
181 		if (!client)
182 			break;
183 
184 		if (i2c_dev == client) {
185 			dev_dbg(&client->adapter->dev, "lis3lv02d i2c_client removed\n");
186 			i2c_dev = NULL;
187 		}
188 		break;
189 	default:
190 		break;
191 	}
192 
193 	return 0;
194 }
195 static struct notifier_block i2c_nb = { .notifier_call = i2c_bus_notify };
196 
197 static int __init match_acpi_device_ids(struct device *dev, const void *data)
198 {
199 	return acpi_match_device(data, dev) ? 1 : 0;
200 }
201 
202 static int __init dell_lis3lv02d_init(void)
203 {
204 	const struct dmi_system_id *lis3lv02d_dmi_id;
205 	struct device *dev;
206 	int err;
207 
208 	/*
209 	 * First check for a matching platform_device. This protects against
210 	 * SMO88xx ACPI fwnodes which actually do have an I2C resource, which
211 	 * will already have an i2c_client instantiated (not a platform_device).
212 	 */
213 	dev = bus_find_device(&platform_bus_type, NULL, smo8800_ids, match_acpi_device_ids);
214 	if (!dev) {
215 		pr_debug("No SMO88xx platform-device found\n");
216 		return 0;
217 	}
218 	put_device(dev);
219 
220 	lis3lv02d_dmi_id = dmi_first_match(lis3lv02d_devices);
221 	if (!lis3lv02d_dmi_id && !probe_i2c_addr) {
222 		pr_warn("accelerometer is present on SMBus but its address is unknown, skipping registration\n");
223 		pr_info("Pass dell_lis3lv02d.probe_i2c_addr=1 on the kernel command line to probe, this may be dangerous!\n");
224 		return 0;
225 	}
226 
227 	if (lis3lv02d_dmi_id)
228 		i2c_addr = (long)lis3lv02d_dmi_id->driver_data;
229 
230 	/*
231 	 * Register i2c-bus notifier + queue initial scan for lis3lv02d
232 	 * i2c_client instantiation.
233 	 */
234 	err = bus_register_notifier(&i2c_bus_type, &i2c_nb);
235 	if (err)
236 		return err;
237 
238 	notifier_registered = true;
239 
240 	queue_work(system_long_wq, &i2c_work);
241 	return 0;
242 }
243 module_init(dell_lis3lv02d_init);
244 
245 static void __exit dell_lis3lv02d_module_exit(void)
246 {
247 	if (!notifier_registered)
248 		return;
249 
250 	bus_unregister_notifier(&i2c_bus_type, &i2c_nb);
251 	cancel_work_sync(&i2c_work);
252 	i2c_unregister_device(i2c_dev);
253 }
254 module_exit(dell_lis3lv02d_module_exit);
255 
256 MODULE_DESCRIPTION("lis3lv02d i2c-client instantiation for ACPI SMO88xx devices");
257 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
258 MODULE_LICENSE("GPL");
259