xref: /linux/drivers/platform/x86/dell/dell-lis3lv02d.c (revision a339dd699a7aa01bce4b38c8d81def310cf2bca0)
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 M6800",    0x29),
53 	DELL_LIS3LV02D_DMI_ENTRY("Vostro V131",        0x1d),
54 	DELL_LIS3LV02D_DMI_ENTRY("Vostro 5568",        0x29),
55 	DELL_LIS3LV02D_DMI_ENTRY("XPS 15 7590",        0x29),
56 	DELL_LIS3LV02D_DMI_ENTRY("XPS 15 9550",        0x29),
57 	{ }
58 };
59 
60 static u8 i2c_addr;
61 static struct i2c_client *i2c_dev;
62 static bool notifier_registered;
63 
64 static bool probe_i2c_addr;
65 module_param(probe_i2c_addr, bool, 0444);
66 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.");
67 
68 static int detect_lis3lv02d(struct i2c_adapter *adap, unsigned short addr)
69 {
70 	union i2c_smbus_data smbus_data;
71 	int err;
72 
73 	dev_info(&adap->dev, "Probing for lis3lv02d on address 0x%02x\n", addr);
74 
75 	err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, LIS3_WHO_AM_I,
76 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
77 	if (err < 0)
78 		return 0; /* Not found */
79 
80 	/* valid who-am-i values are from drivers/misc/lis3lv02d/lis3lv02d.c */
81 	switch (smbus_data.byte) {
82 	case 0x32:
83 	case 0x33:
84 	case 0x3a:
85 	case 0x3b:
86 		break;
87 	default:
88 		dev_warn(&adap->dev, "Unknown who-am-i register value 0x%02x\n",
89 			 smbus_data.byte);
90 		return 0; /* Not found */
91 	}
92 
93 	dev_info(&adap->dev,
94 		 "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",
95 		 addr);
96 
97 	return 1; /* Found */
98 }
99 
100 static bool i2c_adapter_is_main_i801(struct i2c_adapter *adap)
101 {
102 	/*
103 	 * Only match the main I801 adapter and reject secondary adapters
104 	 * which names start with "SMBus I801 IDF adapter".
105 	 */
106 	return strstarts(adap->name, "SMBus I801 adapter");
107 }
108 
109 static int find_i801(struct device *dev, void *data)
110 {
111 	struct i2c_adapter *adap, **adap_ret = data;
112 
113 	adap = i2c_verify_adapter(dev);
114 	if (!adap)
115 		return 0;
116 
117 	if (!i2c_adapter_is_main_i801(adap))
118 		return 0;
119 
120 	*adap_ret = i2c_get_adapter(adap->nr);
121 	return 1;
122 }
123 
124 static void instantiate_i2c_client(struct work_struct *work)
125 {
126 	struct i2c_board_info info = { };
127 	struct i2c_adapter *adap = NULL;
128 
129 	if (i2c_dev)
130 		return;
131 
132 	/*
133 	 * bus_for_each_dev() and not i2c_for_each_dev() to avoid
134 	 * a deadlock when find_i801() calls i2c_get_adapter().
135 	 */
136 	bus_for_each_dev(&i2c_bus_type, NULL, &adap, find_i801);
137 	if (!adap)
138 		return;
139 
140 	strscpy(info.type, "lis3lv02d", I2C_NAME_SIZE);
141 
142 	if (i2c_addr) {
143 		info.addr = i2c_addr;
144 		i2c_dev = i2c_new_client_device(adap, &info);
145 	} else {
146 		/* First try address 0x29 (most used) and then try 0x1d */
147 		static const unsigned short addr_list[] = { 0x29, 0x1d, I2C_CLIENT_END };
148 
149 		i2c_dev = i2c_new_scanned_device(adap, &info, addr_list, detect_lis3lv02d);
150 	}
151 
152 	if (IS_ERR(i2c_dev)) {
153 		dev_err(&adap->dev, "error %ld registering i2c_client\n", PTR_ERR(i2c_dev));
154 		i2c_dev = NULL;
155 	} else {
156 		dev_dbg(&adap->dev, "registered lis3lv02d on address 0x%02x\n", info.addr);
157 	}
158 
159 	i2c_put_adapter(adap);
160 }
161 static DECLARE_WORK(i2c_work, instantiate_i2c_client);
162 
163 static int i2c_bus_notify(struct notifier_block *nb, unsigned long action, void *data)
164 {
165 	struct device *dev = data;
166 	struct i2c_client *client;
167 	struct i2c_adapter *adap;
168 
169 	switch (action) {
170 	case BUS_NOTIFY_ADD_DEVICE:
171 		adap = i2c_verify_adapter(dev);
172 		if (!adap)
173 			break;
174 
175 		if (i2c_adapter_is_main_i801(adap))
176 			queue_work(system_long_wq, &i2c_work);
177 		break;
178 	case BUS_NOTIFY_REMOVED_DEVICE:
179 		client = i2c_verify_client(dev);
180 		if (!client)
181 			break;
182 
183 		if (i2c_dev == client) {
184 			dev_dbg(&client->adapter->dev, "lis3lv02d i2c_client removed\n");
185 			i2c_dev = NULL;
186 		}
187 		break;
188 	default:
189 		break;
190 	}
191 
192 	return 0;
193 }
194 static struct notifier_block i2c_nb = { .notifier_call = i2c_bus_notify };
195 
196 static int __init match_acpi_device_ids(struct device *dev, const void *data)
197 {
198 	return acpi_match_device(data, dev) ? 1 : 0;
199 }
200 
201 static int __init dell_lis3lv02d_init(void)
202 {
203 	const struct dmi_system_id *lis3lv02d_dmi_id;
204 	struct device *dev;
205 	int err;
206 
207 	/*
208 	 * First check for a matching platform_device. This protects against
209 	 * SMO88xx ACPI fwnodes which actually do have an I2C resource, which
210 	 * will already have an i2c_client instantiated (not a platform_device).
211 	 */
212 	dev = bus_find_device(&platform_bus_type, NULL, smo8800_ids, match_acpi_device_ids);
213 	if (!dev) {
214 		pr_debug("No SMO88xx platform-device found\n");
215 		return 0;
216 	}
217 	put_device(dev);
218 
219 	lis3lv02d_dmi_id = dmi_first_match(lis3lv02d_devices);
220 	if (!lis3lv02d_dmi_id && !probe_i2c_addr) {
221 		pr_warn("accelerometer is present on SMBus but its address is unknown, skipping registration\n");
222 		pr_info("Pass dell_lis3lv02d.probe_i2c_addr=1 on the kernel command line to probe, this may be dangerous!\n");
223 		return 0;
224 	}
225 
226 	if (lis3lv02d_dmi_id)
227 		i2c_addr = (long)lis3lv02d_dmi_id->driver_data;
228 
229 	/*
230 	 * Register i2c-bus notifier + queue initial scan for lis3lv02d
231 	 * i2c_client instantiation.
232 	 */
233 	err = bus_register_notifier(&i2c_bus_type, &i2c_nb);
234 	if (err)
235 		return err;
236 
237 	notifier_registered = true;
238 
239 	queue_work(system_long_wq, &i2c_work);
240 	return 0;
241 }
242 module_init(dell_lis3lv02d_init);
243 
244 static void __exit dell_lis3lv02d_module_exit(void)
245 {
246 	if (!notifier_registered)
247 		return;
248 
249 	bus_unregister_notifier(&i2c_bus_type, &i2c_nb);
250 	cancel_work_sync(&i2c_work);
251 	i2c_unregister_device(i2c_dev);
252 }
253 module_exit(dell_lis3lv02d_module_exit);
254 
255 MODULE_DESCRIPTION("lis3lv02d i2c-client instantiation for ACPI SMO88xx devices");
256 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
257 MODULE_LICENSE("GPL");
258