|
| 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 DELL_LIS3LV02D_DMI_ENTRY(product_name, i2c_addr) \ |
| 19 | + { \ |
| 20 | + .matches = { \ |
| 21 | + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), \ |
| 22 | + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, product_name), \ |
| 23 | + }, \ |
| 24 | + .driver_data = (void *)(uintptr_t)(i2c_addr), \ |
| 25 | + } |
| 26 | + |
| 27 | +/* |
| 28 | + * Accelerometer's I2C address is not specified in DMI nor ACPI, |
| 29 | + * so it is needed to define mapping table based on DMI product names. |
| 30 | + */ |
| 31 | +static const struct dmi_system_id lis3lv02d_devices[] __initconst = { |
| 32 | + /* |
| 33 | + * Dell platform team told us that these Latitude devices have |
| 34 | + * ST microelectronics accelerometer at I2C address 0x29. |
| 35 | + */ |
| 36 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E5250", 0x29), |
| 37 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E5450", 0x29), |
| 38 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E5550", 0x29), |
| 39 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E6440", 0x29), |
| 40 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E6440 ATG", 0x29), |
| 41 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude E6540", 0x29), |
| 42 | + /* |
| 43 | + * Additional individual entries were added after verification. |
| 44 | + */ |
| 45 | + DELL_LIS3LV02D_DMI_ENTRY("Latitude 5480", 0x29), |
| 46 | + DELL_LIS3LV02D_DMI_ENTRY("Precision 3540", 0x29), |
| 47 | + DELL_LIS3LV02D_DMI_ENTRY("Vostro V131", 0x1d), |
| 48 | + DELL_LIS3LV02D_DMI_ENTRY("Vostro 5568", 0x29), |
| 49 | + DELL_LIS3LV02D_DMI_ENTRY("XPS 15 7590", 0x29), |
| 50 | + { } |
| 51 | +}; |
| 52 | + |
| 53 | +static u8 i2c_addr; |
| 54 | +static struct i2c_client *i2c_dev; |
| 55 | +static bool notifier_registered; |
| 56 | + |
| 57 | +static bool i2c_adapter_is_main_i801(struct i2c_adapter *adap) |
| 58 | +{ |
| 59 | + /* |
| 60 | + * Only match the main I801 adapter and reject secondary adapters |
| 61 | + * which names start with "SMBus I801 IDF adapter". |
| 62 | + */ |
| 63 | + return strstarts(adap->name, "SMBus I801 adapter"); |
| 64 | +} |
| 65 | + |
| 66 | +static int find_i801(struct device *dev, void *data) |
| 67 | +{ |
| 68 | + struct i2c_adapter *adap, **adap_ret = data; |
| 69 | + |
| 70 | + adap = i2c_verify_adapter(dev); |
| 71 | + if (!adap) |
| 72 | + return 0; |
| 73 | + |
| 74 | + if (!i2c_adapter_is_main_i801(adap)) |
| 75 | + return 0; |
| 76 | + |
| 77 | + *adap_ret = i2c_get_adapter(adap->nr); |
| 78 | + return 1; |
| 79 | +} |
| 80 | + |
| 81 | +static void instantiate_i2c_client(struct work_struct *work) |
| 82 | +{ |
| 83 | + struct i2c_board_info info = { }; |
| 84 | + struct i2c_adapter *adap = NULL; |
| 85 | + |
| 86 | + if (i2c_dev) |
| 87 | + return; |
| 88 | + |
| 89 | + /* |
| 90 | + * bus_for_each_dev() and not i2c_for_each_dev() to avoid |
| 91 | + * a deadlock when find_i801() calls i2c_get_adapter(). |
| 92 | + */ |
| 93 | + bus_for_each_dev(&i2c_bus_type, NULL, &adap, find_i801); |
| 94 | + if (!adap) |
| 95 | + return; |
| 96 | + |
| 97 | + info.addr = i2c_addr; |
| 98 | + strscpy(info.type, "lis3lv02d", I2C_NAME_SIZE); |
| 99 | + |
| 100 | + i2c_dev = i2c_new_client_device(adap, &info); |
| 101 | + if (IS_ERR(i2c_dev)) { |
| 102 | + dev_err(&adap->dev, "error %ld registering i2c_client\n", PTR_ERR(i2c_dev)); |
| 103 | + i2c_dev = NULL; |
| 104 | + } else { |
| 105 | + dev_dbg(&adap->dev, "registered lis3lv02d on address 0x%02x\n", info.addr); |
| 106 | + } |
| 107 | + |
| 108 | + i2c_put_adapter(adap); |
| 109 | +} |
| 110 | +static DECLARE_WORK(i2c_work, instantiate_i2c_client); |
| 111 | + |
| 112 | +static int i2c_bus_notify(struct notifier_block *nb, unsigned long action, void *data) |
| 113 | +{ |
| 114 | + struct device *dev = data; |
| 115 | + struct i2c_client *client; |
| 116 | + struct i2c_adapter *adap; |
| 117 | + |
| 118 | + switch (action) { |
| 119 | + case BUS_NOTIFY_ADD_DEVICE: |
| 120 | + adap = i2c_verify_adapter(dev); |
| 121 | + if (!adap) |
| 122 | + break; |
| 123 | + |
| 124 | + if (i2c_adapter_is_main_i801(adap)) |
| 125 | + queue_work(system_long_wq, &i2c_work); |
| 126 | + break; |
| 127 | + case BUS_NOTIFY_REMOVED_DEVICE: |
| 128 | + client = i2c_verify_client(dev); |
| 129 | + if (!client) |
| 130 | + break; |
| 131 | + |
| 132 | + if (i2c_dev == client) { |
| 133 | + dev_dbg(&client->adapter->dev, "lis3lv02d i2c_client removed\n"); |
| 134 | + i2c_dev = NULL; |
| 135 | + } |
| 136 | + break; |
| 137 | + default: |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | +static struct notifier_block i2c_nb = { .notifier_call = i2c_bus_notify }; |
| 144 | + |
| 145 | +static int __init match_acpi_device_ids(struct device *dev, const void *data) |
| 146 | +{ |
| 147 | + return acpi_match_device(data, dev) ? 1 : 0; |
| 148 | +} |
| 149 | + |
| 150 | +static int __init dell_lis3lv02d_init(void) |
| 151 | +{ |
| 152 | + const struct dmi_system_id *lis3lv02d_dmi_id; |
| 153 | + struct device *dev; |
| 154 | + int err; |
| 155 | + |
| 156 | + /* |
| 157 | + * First check for a matching platform_device. This protects against |
| 158 | + * SMO88xx ACPI fwnodes which actually do have an I2C resource, which |
| 159 | + * will already have an i2c_client instantiated (not a platform_device). |
| 160 | + */ |
| 161 | + dev = bus_find_device(&platform_bus_type, NULL, smo8800_ids, match_acpi_device_ids); |
| 162 | + if (!dev) { |
| 163 | + pr_debug("No SMO88xx platform-device found\n"); |
| 164 | + return 0; |
| 165 | + } |
| 166 | + put_device(dev); |
| 167 | + |
| 168 | + lis3lv02d_dmi_id = dmi_first_match(lis3lv02d_devices); |
| 169 | + if (!lis3lv02d_dmi_id) { |
| 170 | + pr_warn("accelerometer is present on SMBus but its address is unknown, skipping registration\n"); |
| 171 | + return 0; |
| 172 | + } |
| 173 | + |
| 174 | + i2c_addr = (long)lis3lv02d_dmi_id->driver_data; |
| 175 | + |
| 176 | + /* |
| 177 | + * Register i2c-bus notifier + queue initial scan for lis3lv02d |
| 178 | + * i2c_client instantiation. |
| 179 | + */ |
| 180 | + err = bus_register_notifier(&i2c_bus_type, &i2c_nb); |
| 181 | + if (err) |
| 182 | + return err; |
| 183 | + |
| 184 | + notifier_registered = true; |
| 185 | + |
| 186 | + queue_work(system_long_wq, &i2c_work); |
| 187 | + return 0; |
| 188 | +} |
| 189 | +module_init(dell_lis3lv02d_init); |
| 190 | + |
| 191 | +static void __exit dell_lis3lv02d_module_exit(void) |
| 192 | +{ |
| 193 | + if (!notifier_registered) |
| 194 | + return; |
| 195 | + |
| 196 | + bus_unregister_notifier(&i2c_bus_type, &i2c_nb); |
| 197 | + cancel_work_sync(&i2c_work); |
| 198 | + i2c_unregister_device(i2c_dev); |
| 199 | +} |
| 200 | +module_exit(dell_lis3lv02d_module_exit); |
| 201 | + |
| 202 | +MODULE_DESCRIPTION("lis3lv02d i2c-client instantiation for ACPI SMO88xx devices"); |
| 203 | +MODULE_AUTHOR( "Hans de Goede <[email protected]>"); |
| 204 | +MODULE_LICENSE("GPL"); |
0 commit comments