1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux I2C core ACPI support code
8 #include <linux/acpi.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
18 struct i2c_acpi_handler_data {
19 struct acpi_connection_info info;
20 struct i2c_adapter *adapter;
33 struct i2c_acpi_lookup {
34 struct i2c_board_info *info;
35 acpi_handle adapter_handle;
36 acpi_handle device_handle;
37 acpi_handle search_handle;
45 * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
46 * @ares: ACPI resource
47 * @i2c: Pointer to I2cSerialBus resource will be returned here
49 * Checks if the given ACPI resource is of type I2cSerialBus.
50 * In this case, returns a pointer to it to the caller.
52 * Returns true if resource type is of I2cSerialBus, otherwise false.
54 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
55 struct acpi_resource_i2c_serialbus **i2c)
57 struct acpi_resource_i2c_serialbus *sb;
59 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
62 sb = &ares->data.i2c_serial_bus;
63 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
69 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
71 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
73 struct i2c_acpi_lookup *lookup = data;
74 struct i2c_board_info *info = lookup->info;
75 struct acpi_resource_i2c_serialbus *sb;
78 if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
81 if (lookup->index != -1 && lookup->n++ != lookup->index)
84 status = acpi_get_handle(lookup->device_handle,
85 sb->resource_source.string_ptr,
86 &lookup->adapter_handle);
87 if (ACPI_FAILURE(status))
90 info->addr = sb->slave_address;
91 lookup->speed = sb->connection_speed;
92 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
93 info->flags |= I2C_CLIENT_TEN;
98 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
100 * ACPI video acpi_devices, which are handled by the acpi-video driver
101 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
103 { ACPI_VIDEO_HID, 0 },
107 static int i2c_acpi_do_lookup(struct acpi_device *adev,
108 struct i2c_acpi_lookup *lookup)
110 struct i2c_board_info *info = lookup->info;
111 struct list_head resource_list;
114 if (acpi_bus_get_status(adev) || !adev->status.present ||
115 acpi_device_enumerated(adev))
118 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
121 memset(info, 0, sizeof(*info));
122 lookup->device_handle = acpi_device_handle(adev);
124 /* Look up for I2cSerialBus resource */
125 INIT_LIST_HEAD(&resource_list);
126 ret = acpi_dev_get_resources(adev, &resource_list,
127 i2c_acpi_fill_info, lookup);
128 acpi_dev_free_resource_list(&resource_list);
130 if (ret < 0 || !info->addr)
136 static int i2c_acpi_get_info(struct acpi_device *adev,
137 struct i2c_board_info *info,
138 struct i2c_adapter *adapter,
139 acpi_handle *adapter_handle)
141 struct list_head resource_list;
142 struct resource_entry *entry;
143 struct i2c_acpi_lookup lookup;
146 memset(&lookup, 0, sizeof(lookup));
150 ret = i2c_acpi_do_lookup(adev, &lookup);
155 /* The adapter must match the one in I2cSerialBus() connector */
156 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
159 struct acpi_device *adapter_adev;
161 /* The adapter must be present */
162 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
164 if (acpi_bus_get_status(adapter_adev) ||
165 !adapter_adev->status.present)
169 info->fwnode = acpi_fwnode_handle(adev);
171 *adapter_handle = lookup.adapter_handle;
173 /* Then fill IRQ number if any */
174 INIT_LIST_HEAD(&resource_list);
175 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
179 resource_list_for_each_entry(entry, &resource_list) {
180 if (resource_type(entry->res) == IORESOURCE_IRQ) {
181 info->irq = entry->res->start;
186 acpi_dev_free_resource_list(&resource_list);
188 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
194 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
195 struct acpi_device *adev,
196 struct i2c_board_info *info)
198 adev->power.flags.ignore_parent = true;
199 acpi_device_set_enumerated(adev);
201 if (!i2c_new_device(adapter, info)) {
202 adev->power.flags.ignore_parent = false;
203 dev_err(&adapter->dev,
204 "failed to add I2C device %s from ACPI\n",
205 dev_name(&adev->dev));
209 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
210 void *data, void **return_value)
212 struct i2c_adapter *adapter = data;
213 struct acpi_device *adev;
214 struct i2c_board_info info;
216 if (acpi_bus_get_device(handle, &adev))
219 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
222 i2c_acpi_register_device(adapter, adev, &info);
227 #define I2C_ACPI_MAX_SCAN_DEPTH 32
230 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
231 * @adap: pointer to adapter
233 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
234 * namespace. When a device is found it will be added to the Linux device
235 * model and bound to the corresponding ACPI handle.
237 void i2c_acpi_register_devices(struct i2c_adapter *adap)
241 if (!has_acpi_companion(&adap->dev))
244 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
245 I2C_ACPI_MAX_SCAN_DEPTH,
246 i2c_acpi_add_device, NULL,
248 if (ACPI_FAILURE(status))
249 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
252 const struct acpi_device_id *
253 i2c_acpi_match_device(const struct acpi_device_id *matches,
254 struct i2c_client *client)
256 if (!(client && matches))
259 return acpi_match_device(matches, &client->dev);
262 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
263 void *data, void **return_value)
265 struct i2c_acpi_lookup *lookup = data;
266 struct acpi_device *adev;
268 if (acpi_bus_get_device(handle, &adev))
271 if (i2c_acpi_do_lookup(adev, lookup))
274 if (lookup->search_handle != lookup->adapter_handle)
277 if (lookup->speed <= lookup->min_speed)
278 lookup->min_speed = lookup->speed;
284 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
285 * @dev: The device owning the bus
287 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
288 * devices connected to this bus and use the speed of slowest device.
290 * Returns the speed in Hz or zero
292 u32 i2c_acpi_find_bus_speed(struct device *dev)
294 struct i2c_acpi_lookup lookup;
295 struct i2c_board_info dummy;
298 if (!has_acpi_companion(dev))
301 memset(&lookup, 0, sizeof(lookup));
302 lookup.search_handle = ACPI_HANDLE(dev);
303 lookup.min_speed = UINT_MAX;
304 lookup.info = &dummy;
307 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
308 I2C_ACPI_MAX_SCAN_DEPTH,
309 i2c_acpi_lookup_speed, NULL,
312 if (ACPI_FAILURE(status)) {
313 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
317 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
319 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
321 static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
323 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
328 return ACPI_HANDLE(dev) == (acpi_handle)data;
331 static int i2c_acpi_find_match_device(struct device *dev, void *data)
333 return ACPI_COMPANION(dev) == data;
336 static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
340 dev = bus_find_device(&i2c_bus_type, NULL, handle,
341 i2c_acpi_find_match_adapter);
342 return dev ? i2c_verify_adapter(dev) : NULL;
345 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
349 dev = bus_find_device(&i2c_bus_type, NULL, adev,
350 i2c_acpi_find_match_device);
351 return dev ? i2c_verify_client(dev) : NULL;
354 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
357 struct acpi_device *adev = arg;
358 struct i2c_board_info info;
359 acpi_handle adapter_handle;
360 struct i2c_adapter *adapter;
361 struct i2c_client *client;
364 case ACPI_RECONFIG_DEVICE_ADD:
365 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
368 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
372 i2c_acpi_register_device(adapter, adev, &info);
374 case ACPI_RECONFIG_DEVICE_REMOVE:
375 if (!acpi_device_enumerated(adev))
378 client = i2c_acpi_find_client_by_adev(adev);
382 i2c_unregister_device(client);
383 put_device(&client->dev);
390 struct notifier_block i2c_acpi_notifier = {
391 .notifier_call = i2c_acpi_notify,
395 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
396 * @dev: Device owning the ACPI resources to get the client from
397 * @index: Index of ACPI resource to get
398 * @info: describes the I2C device; note this is modified (addr gets set)
401 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
402 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
403 * resources, in that case this function can be used to create an i2c-client
404 * for other I2cSerialBus resources in the Current Resource Settings table.
406 * Also see i2c_new_device, which this function calls to create the i2c-client.
408 * Returns a pointer to the new i2c-client, or error pointer in case of failure.
409 * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
411 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
412 struct i2c_board_info *info)
414 struct i2c_acpi_lookup lookup;
415 struct i2c_adapter *adapter;
416 struct i2c_client *client;
417 struct acpi_device *adev;
418 LIST_HEAD(resource_list);
421 adev = ACPI_COMPANION(dev);
423 return ERR_PTR(-EINVAL);
425 memset(&lookup, 0, sizeof(lookup));
427 lookup.device_handle = acpi_device_handle(adev);
428 lookup.index = index;
430 ret = acpi_dev_get_resources(adev, &resource_list,
431 i2c_acpi_fill_info, &lookup);
435 acpi_dev_free_resource_list(&resource_list);
438 return ERR_PTR(-EADDRNOTAVAIL);
440 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
442 return ERR_PTR(-EPROBE_DEFER);
444 client = i2c_new_device(adapter, info);
446 return ERR_PTR(-ENODEV);
450 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
452 #ifdef CONFIG_ACPI_I2C_OPREGION
453 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
454 u8 cmd, u8 *data, u8 data_len)
457 struct i2c_msg msgs[2];
461 buffer = kzalloc(data_len, GFP_KERNEL);
465 msgs[0].addr = client->addr;
466 msgs[0].flags = client->flags;
470 msgs[1].addr = client->addr;
471 msgs[1].flags = client->flags | I2C_M_RD;
472 msgs[1].len = data_len;
473 msgs[1].buf = buffer;
475 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
477 /* Getting a NACK is unfortunately normal with some DSTDs */
478 if (ret == -EREMOTEIO)
479 dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
480 data_len, client->addr, cmd, ret);
482 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
483 data_len, client->addr, cmd, ret);
484 /* 2 transfers must have completed successfully */
485 } else if (ret == 2) {
486 memcpy(data, buffer, data_len);
496 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
497 u8 cmd, u8 *data, u8 data_len)
500 struct i2c_msg msgs[1];
504 buffer = kzalloc(data_len + 1, GFP_KERNEL);
509 memcpy(buffer + 1, data, data_len);
511 msgs[0].addr = client->addr;
512 msgs[0].flags = client->flags;
513 msgs[0].len = data_len + 1;
514 msgs[0].buf = buffer;
516 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
521 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
525 /* 1 transfer must have completed successfully */
526 return (ret == 1) ? 0 : -EIO;
530 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
531 u32 bits, u64 *value64,
532 void *handler_context, void *region_context)
534 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
535 struct i2c_acpi_handler_data *data = handler_context;
536 struct acpi_connection_info *info = &data->info;
537 struct acpi_resource_i2c_serialbus *sb;
538 struct i2c_adapter *adapter = data->adapter;
539 struct i2c_client *client;
540 struct acpi_resource *ares;
541 u32 accessor_type = function >> 16;
542 u8 action = function & ACPI_IO_MASK;
546 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
547 if (ACPI_FAILURE(ret))
550 client = kzalloc(sizeof(*client), GFP_KERNEL);
556 if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
557 ret = AE_BAD_PARAMETER;
561 client->adapter = adapter;
562 client->addr = sb->slave_address;
564 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
565 client->flags |= I2C_CLIENT_TEN;
567 switch (accessor_type) {
568 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
569 if (action == ACPI_READ) {
570 status = i2c_smbus_read_byte(client);
576 status = i2c_smbus_write_byte(client, gsb->bdata);
580 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
581 if (action == ACPI_READ) {
582 status = i2c_smbus_read_byte_data(client, command);
588 status = i2c_smbus_write_byte_data(client, command,
593 case ACPI_GSB_ACCESS_ATTRIB_WORD:
594 if (action == ACPI_READ) {
595 status = i2c_smbus_read_word_data(client, command);
601 status = i2c_smbus_write_word_data(client, command,
606 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
607 if (action == ACPI_READ) {
608 status = i2c_smbus_read_block_data(client, command,
615 status = i2c_smbus_write_block_data(client, command,
616 gsb->len, gsb->data);
620 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
621 if (action == ACPI_READ) {
622 status = acpi_gsb_i2c_read_bytes(client, command,
623 gsb->data, info->access_length);
625 status = acpi_gsb_i2c_write_bytes(client, command,
626 gsb->data, info->access_length);
631 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
632 accessor_type, client->addr);
633 ret = AE_BAD_PARAMETER;
637 gsb->status = status;
646 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
649 struct i2c_acpi_handler_data *data;
652 if (!adapter->dev.parent)
655 handle = ACPI_HANDLE(adapter->dev.parent);
660 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
665 data->adapter = adapter;
666 status = acpi_bus_attach_private_data(handle, (void *)data);
667 if (ACPI_FAILURE(status)) {
672 status = acpi_install_address_space_handler(handle,
673 ACPI_ADR_SPACE_GSBUS,
674 &i2c_acpi_space_handler,
677 if (ACPI_FAILURE(status)) {
678 dev_err(&adapter->dev, "Error installing i2c space handler\n");
679 acpi_bus_detach_private_data(handle);
684 acpi_walk_dep_device_list(handle);
688 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
691 struct i2c_acpi_handler_data *data;
694 if (!adapter->dev.parent)
697 handle = ACPI_HANDLE(adapter->dev.parent);
702 acpi_remove_address_space_handler(handle,
703 ACPI_ADR_SPACE_GSBUS,
704 &i2c_acpi_space_handler);
706 status = acpi_bus_get_private_data(handle, (void **)&data);
707 if (ACPI_SUCCESS(status))
710 acpi_bus_detach_private_data(handle);
712 #endif /* CONFIG_ACPI_I2C_OPREGION */