}
#if defined(CONFIG_ACPI)
-struct i2c_acpi_handler_data {
+struct acpi_i2c_handler_data {
struct acpi_connection_info info;
struct i2c_adapter *adapter;
};
};
} __packed;
-struct i2c_acpi_lookup {
+struct acpi_i2c_lookup {
struct i2c_board_info *info;
- struct i2c_adapter *adapter; /* set only when registering slaves */
acpi_handle adapter_handle;
acpi_handle device_handle;
- u32 min_speed;
};
-static int i2c_acpi_find_resource(struct acpi_resource *ares, void *data)
+static int acpi_i2c_fill_info(struct acpi_resource *ares, void *data)
{
- struct i2c_acpi_lookup *lookup = data;
+ struct acpi_i2c_lookup *lookup = data;
struct i2c_board_info *info = lookup->info;
struct acpi_resource_i2c_serialbus *sb;
- acpi_handle adapter_handle;
acpi_status status;
if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
return 1;
- /*
- * Extract the ResourceSource and make sure that the handle matches
- * with the I2C adapter handle.
- */
status = acpi_get_handle(lookup->device_handle,
sb->resource_source.string_ptr,
- &adapter_handle);
- if (ACPI_SUCCESS(status) && adapter_handle == lookup->adapter_handle) {
- info->addr = sb->slave_address;
- if (sb->access_mode == ACPI_I2C_10BIT_MODE)
- info->flags |= I2C_CLIENT_TEN;
- /* Save speed of the slowest device */
- if (sb->connection_speed < lookup->min_speed)
- lookup->min_speed = sb->connection_speed;
- }
+ &lookup->adapter_handle);
+ if (!ACPI_SUCCESS(status))
+ return 1;
+
+ info->addr = sb->slave_address;
+ if (sb->access_mode == ACPI_I2C_10BIT_MODE)
+ info->flags |= I2C_CLIENT_TEN;
return 1;
}
-static acpi_status i2c_acpi_slave_lookup(acpi_handle handle, u32 level,
- void *data, void **return_value)
+static int acpi_i2c_get_info(struct acpi_device *adev,
+ struct i2c_board_info *info,
+ acpi_handle *adapter_handle)
{
- struct i2c_acpi_lookup *lookup = data;
- struct i2c_adapter *adapter = lookup->adapter;
struct list_head resource_list;
struct resource_entry *entry;
- struct i2c_board_info info;
- struct acpi_device *adev;
+ struct acpi_i2c_lookup lookup;
int ret;
- if (acpi_bus_get_device(handle, &adev))
- return AE_OK;
- if (acpi_bus_get_status(adev) || !adev->status.present)
- return AE_OK;
+ if (acpi_bus_get_status(adev) || !adev->status.present ||
+ acpi_device_enumerated(adev))
+ return -EINVAL;
- memset(&info, 0, sizeof(info));
- info.fwnode = acpi_fwnode_handle(adev);
+ memset(info, 0, sizeof(*info));
+ info->fwnode = acpi_fwnode_handle(adev);
- lookup->device_handle = handle;
- lookup->info = &info;
+ memset(&lookup, 0, sizeof(lookup));
+ lookup.device_handle = acpi_device_handle(adev);
+ lookup.info = info;
- /*
- * Look up for I2cSerialBus resource with ResourceSource that
- * matches with this adapter.
- */
+ /* Look up for I2cSerialBus resource */
INIT_LIST_HEAD(&resource_list);
ret = acpi_dev_get_resources(adev, &resource_list,
- i2c_acpi_find_resource, lookup);
+ acpi_i2c_fill_info, &lookup);
acpi_dev_free_resource_list(&resource_list);
- if (ret < 0 || !info.addr || !lookup->adapter)
- return AE_OK;
+ if (ret < 0 || !info->addr)
+ return -EINVAL;
+
+ *adapter_handle = lookup.adapter_handle;
/* Then fill IRQ number if any */
ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
if (ret < 0)
- return AE_OK;
+ return -EINVAL;
resource_list_for_each_entry(entry, &resource_list) {
if (resource_type(entry->res) == IORESOURCE_IRQ) {
- info.irq = entry->res->start;
+ info->irq = entry->res->start;
break;
}
}
acpi_dev_free_resource_list(&resource_list);
+ strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
+
+ return 0;
+}
+
+static void acpi_i2c_register_device(struct i2c_adapter *adapter,
+ struct acpi_device *adev,
+ struct i2c_board_info *info)
+{
adev->power.flags.ignore_parent = true;
- strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
- if (!i2c_new_device(adapter, &info)) {
+ acpi_device_set_enumerated(adev);
+
+ if (!i2c_new_device(adapter, info)) {
adev->power.flags.ignore_parent = false;
dev_err(&adapter->dev,
"failed to add I2C device %s from ACPI\n",
dev_name(&adev->dev));
}
-
- return AE_OK;
}
-#define I2C_ACPI_MAX_SCAN_DEPTH 32
-
-static acpi_status i2c_acpi_walk(struct i2c_acpi_lookup *lookup)
+static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
+ void *data, void **return_value)
{
- return acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
- I2C_ACPI_MAX_SCAN_DEPTH,
- i2c_acpi_slave_lookup, NULL,
- lookup, NULL);
+ struct i2c_adapter *adapter = data;
+ struct acpi_device *adev;
+ acpi_handle adapter_handle;
+ struct i2c_board_info info;
+
+ if (acpi_bus_get_device(handle, &adev))
+ return AE_OK;
+
+ if (acpi_i2c_get_info(adev, &info, &adapter_handle))
+ return AE_OK;
+
+ if (adapter_handle != ACPI_HANDLE(&adapter->dev))
+ return AE_OK;
+
+ acpi_i2c_register_device(adapter, adev, &info);
+
+ return AE_OK;
}
+#define ACPI_I2C_MAX_SCAN_DEPTH 32
+
/**
- * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
+ * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
* @adap: pointer to adapter
*
* Enumerate all I2C slave devices behind this adapter by walking the ACPI
* namespace. When a device is found it will be added to the Linux device
* model and bound to the corresponding ACPI handle.
*/
-static void i2c_acpi_register_devices(struct i2c_adapter *adap)
+static void acpi_i2c_register_devices(struct i2c_adapter *adap)
{
- struct i2c_acpi_lookup lookup;
acpi_status status;
if (!has_acpi_companion(&adap->dev))
return;
- memset(&lookup, 0, sizeof(lookup));
- lookup.adapter = adap;
- lookup.adapter_handle = ACPI_HANDLE(&adap->dev);
-
- status = i2c_acpi_walk(&lookup);
+ status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
+ ACPI_I2C_MAX_SCAN_DEPTH,
+ acpi_i2c_add_device, NULL,
+ adap, NULL);
if (ACPI_FAILURE(status))
dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
}
-/**
- * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
- * @dev: The device owning the bus
- *
- * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
- * devices connected to this bus and use the speed of slowest device.
- *
- * Returns the speed in Hz or zero
- */
-u32 i2c_acpi_find_bus_speed(struct device *dev)
+static int acpi_i2c_match_adapter(struct device *dev, void *data)
{
- struct i2c_acpi_lookup lookup;
- acpi_status status;
+ struct i2c_adapter *adapter = i2c_verify_adapter(dev);
- if (!has_acpi_companion(dev))
+ if (!adapter)
return 0;
- memset(&lookup, 0, sizeof(lookup));
- lookup.adapter_handle = ACPI_HANDLE(dev);
- lookup.min_speed = UINT_MAX;
+ return ACPI_HANDLE(dev) == (acpi_handle)data;
+}
- status = i2c_acpi_walk(&lookup);
- if (ACPI_FAILURE(status)) {
- dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
- return 0;
+static int acpi_i2c_match_device(struct device *dev, void *data)
+{
+ return ACPI_COMPANION(dev) == data;
+}
+
+static struct i2c_adapter *acpi_i2c_find_adapter_by_handle(acpi_handle handle)
+{
+ struct device *dev;
+
+ dev = bus_find_device(&i2c_bus_type, NULL, handle,
+ acpi_i2c_match_adapter);
+ return dev ? i2c_verify_adapter(dev) : NULL;
+}
+
+static struct i2c_client *acpi_i2c_find_client_by_adev(struct acpi_device *adev)
+{
+ struct device *dev;
+
+ dev = bus_find_device(&i2c_bus_type, NULL, adev, acpi_i2c_match_device);
+ return dev ? i2c_verify_client(dev) : NULL;
+}
+
+static int acpi_i2c_notify(struct notifier_block *nb, unsigned long value,
+ void *arg)
+{
+ struct acpi_device *adev = arg;
+ struct i2c_board_info info;
+ acpi_handle adapter_handle;
+ struct i2c_adapter *adapter;
+ struct i2c_client *client;
+
+ switch (value) {
+ case ACPI_RECONFIG_DEVICE_ADD:
+ if (acpi_i2c_get_info(adev, &info, &adapter_handle))
+ break;
+
+ adapter = acpi_i2c_find_adapter_by_handle(adapter_handle);
+ if (!adapter)
+ break;
+
+ acpi_i2c_register_device(adapter, adev, &info);
+ break;
+ case ACPI_RECONFIG_DEVICE_REMOVE:
+ if (!acpi_device_enumerated(adev))
+ break;
+
+ client = acpi_i2c_find_client_by_adev(adev);
+ if (!client)
+ break;
+
+ i2c_unregister_device(client);
+ put_device(&client->dev);
+ break;
}
- return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
+ return NOTIFY_OK;
}
-EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
+static struct notifier_block i2c_acpi_notifier = {
+ .notifier_call = acpi_i2c_notify,
+};
#else /* CONFIG_ACPI */
-static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
+static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
+extern struct notifier_block i2c_acpi_notifier;
#endif /* CONFIG_ACPI */
#ifdef CONFIG_ACPI_I2C_OPREGION
}
static acpi_status
-i2c_acpi_space_handler(u32 function, acpi_physical_address command,
+acpi_i2c_space_handler(u32 function, acpi_physical_address command,
u32 bits, u64 *value64,
void *handler_context, void *region_context)
{
struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
- struct i2c_acpi_handler_data *data = handler_context;
+ struct acpi_i2c_handler_data *data = handler_context;
struct acpi_connection_info *info = &data->info;
struct acpi_resource_i2c_serialbus *sb;
struct i2c_adapter *adapter = data->adapter;
}
-static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
+static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
{
acpi_handle handle;
- struct i2c_acpi_handler_data *data;
+ struct acpi_i2c_handler_data *data;
acpi_status status;
if (!adapter->dev.parent)
if (!handle)
return -ENODEV;
- data = kzalloc(sizeof(struct i2c_acpi_handler_data),
+ data = kzalloc(sizeof(struct acpi_i2c_handler_data),
GFP_KERNEL);
if (!data)
return -ENOMEM;
status = acpi_install_address_space_handler(handle,
ACPI_ADR_SPACE_GSBUS,
- &i2c_acpi_space_handler,
+ &acpi_i2c_space_handler,
NULL,
data);
if (ACPI_FAILURE(status)) {
return 0;
}
-static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
+static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
{
acpi_handle handle;
- struct i2c_acpi_handler_data *data;
+ struct acpi_i2c_handler_data *data;
acpi_status status;
if (!adapter->dev.parent)
acpi_remove_address_space_handler(handle,
ACPI_ADR_SPACE_GSBUS,
- &i2c_acpi_space_handler);
+ &acpi_i2c_space_handler);
status = acpi_bus_get_private_data(handle, (void **)&data);
if (ACPI_SUCCESS(status))
acpi_bus_detach_private_data(handle);
}
#else /* CONFIG_ACPI_I2C_OPREGION */
-static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
+static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
{ }
-static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
+static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
{ return 0; }
#endif /* CONFIG_ACPI_I2C_OPREGION */
{
if (client->dev.of_node)
of_node_clear_flag(client->dev.of_node, OF_POPULATED);
+ if (ACPI_COMPANION(&client->dev))
+ acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
device_unregister(&client->dev);
}
EXPORT_SYMBOL_GPL(i2c_unregister_device);
/* create pre-declared device nodes */
of_i2c_register_devices(adap);
- i2c_acpi_register_devices(adap);
- i2c_acpi_install_space_handler(adap);
+ acpi_i2c_register_devices(adap);
+ acpi_i2c_install_space_handler(adap);
if (adap->nr < __i2c_first_dynamic_bus_num)
i2c_scan_static_board_info(adap);
return;
}
- i2c_acpi_remove_space_handler(adap);
+ acpi_i2c_remove_space_handler(adap);
/* Tell drivers about this removal */
mutex_lock(&core_lock);
bus_for_each_drv(&i2c_bus_type, NULL, adap,
if (IS_ENABLED(CONFIG_OF_DYNAMIC))
WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
+ if (IS_ENABLED(CONFIG_ACPI))
+ WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
return 0;
static void __exit i2c_exit(void)
{
+ if (IS_ENABLED(CONFIG_ACPI))
+ WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
if (IS_ENABLED(CONFIG_OF_DYNAMIC))
WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
i2c_del_driver(&dummy_driver);