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