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