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