2 * Linux I2C core ACPI support code
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
12 #include <linux/acpi.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
22 struct i2c_acpi_handler_data {
23 struct acpi_connection_info info;
24 struct i2c_adapter *adapter;
37 struct i2c_acpi_lookup {
38 struct i2c_board_info *info;
39 acpi_handle adapter_handle;
40 acpi_handle device_handle;
41 acpi_handle search_handle;
49 * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
50 * @ares: ACPI resource
51 * @i2c: Pointer to I2cSerialBus resource will be returned here
53 * Checks if the given ACPI resource is of type I2cSerialBus.
54 * In this case, returns a pointer to it to the caller.
56 * Returns true if resource type is of I2cSerialBus, otherwise false.
58 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
59 struct acpi_resource_i2c_serialbus **i2c)
61 struct acpi_resource_i2c_serialbus *sb;
63 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
66 sb = &ares->data.i2c_serial_bus;
67 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
73 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
75 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
77 struct i2c_acpi_lookup *lookup = data;
78 struct i2c_board_info *info = lookup->info;
79 struct acpi_resource_i2c_serialbus *sb;
82 if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
85 if (lookup->index != -1 && lookup->n++ != lookup->index)
88 status = acpi_get_handle(lookup->device_handle,
89 sb->resource_source.string_ptr,
90 &lookup->adapter_handle);
91 if (ACPI_FAILURE(status))
94 info->addr = sb->slave_address;
95 lookup->speed = sb->connection_speed;
96 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
97 info->flags |= I2C_CLIENT_TEN;
102 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
104 * ACPI video acpi_devices, which are handled by the acpi-video driver
105 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
107 { ACPI_VIDEO_HID, 0 },
111 static int i2c_acpi_do_lookup(struct acpi_device *adev,
112 struct i2c_acpi_lookup *lookup)
114 struct i2c_board_info *info = lookup->info;
115 struct list_head resource_list;
118 if (acpi_bus_get_status(adev) || !adev->status.present ||
119 acpi_device_enumerated(adev))
122 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
125 memset(info, 0, sizeof(*info));
126 lookup->device_handle = acpi_device_handle(adev);
128 /* Look up for I2cSerialBus resource */
129 INIT_LIST_HEAD(&resource_list);
130 ret = acpi_dev_get_resources(adev, &resource_list,
131 i2c_acpi_fill_info, lookup);
132 acpi_dev_free_resource_list(&resource_list);
134 if (ret < 0 || !info->addr)
140 static int i2c_acpi_get_info(struct acpi_device *adev,
141 struct i2c_board_info *info,
142 struct i2c_adapter *adapter,
143 acpi_handle *adapter_handle)
145 struct list_head resource_list;
146 struct resource_entry *entry;
147 struct i2c_acpi_lookup lookup;
150 memset(&lookup, 0, sizeof(lookup));
154 ret = i2c_acpi_do_lookup(adev, &lookup);
159 /* The adapter must match the one in I2cSerialBus() connector */
160 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
163 struct acpi_device *adapter_adev;
165 /* The adapter must be present */
166 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
168 if (acpi_bus_get_status(adapter_adev) ||
169 !adapter_adev->status.present)
173 info->fwnode = acpi_fwnode_handle(adev);
175 *adapter_handle = lookup.adapter_handle;
177 /* Then fill IRQ number if any */
178 INIT_LIST_HEAD(&resource_list);
179 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
183 resource_list_for_each_entry(entry, &resource_list) {
184 if (resource_type(entry->res) == IORESOURCE_IRQ) {
185 info->irq = entry->res->start;
190 acpi_dev_free_resource_list(&resource_list);
192 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
198 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
199 struct acpi_device *adev,
200 struct i2c_board_info *info)
202 adev->power.flags.ignore_parent = true;
203 acpi_device_set_enumerated(adev);
205 if (!i2c_new_device(adapter, info)) {
206 adev->power.flags.ignore_parent = false;
207 dev_err(&adapter->dev,
208 "failed to add I2C device %s from ACPI\n",
209 dev_name(&adev->dev));
213 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
214 void *data, void **return_value)
216 struct i2c_adapter *adapter = data;
217 struct acpi_device *adev;
218 struct i2c_board_info info;
220 if (acpi_bus_get_device(handle, &adev))
223 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
226 i2c_acpi_register_device(adapter, adev, &info);
231 #define I2C_ACPI_MAX_SCAN_DEPTH 32
234 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
235 * @adap: pointer to adapter
237 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
238 * namespace. When a device is found it will be added to the Linux device
239 * model and bound to the corresponding ACPI handle.
241 void i2c_acpi_register_devices(struct i2c_adapter *adap)
245 if (!has_acpi_companion(&adap->dev))
248 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
249 I2C_ACPI_MAX_SCAN_DEPTH,
250 i2c_acpi_add_device, NULL,
252 if (ACPI_FAILURE(status))
253 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
256 const struct acpi_device_id *
257 i2c_acpi_match_device(const struct acpi_device_id *matches,
258 struct i2c_client *client)
260 if (!(client && matches))
263 return acpi_match_device(matches, &client->dev);
266 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
267 void *data, void **return_value)
269 struct i2c_acpi_lookup *lookup = data;
270 struct acpi_device *adev;
272 if (acpi_bus_get_device(handle, &adev))
275 if (i2c_acpi_do_lookup(adev, lookup))
278 if (lookup->search_handle != lookup->adapter_handle)
281 if (lookup->speed <= lookup->min_speed)
282 lookup->min_speed = lookup->speed;
288 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
289 * @dev: The device owning the bus
291 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
292 * devices connected to this bus and use the speed of slowest device.
294 * Returns the speed in Hz or zero
296 u32 i2c_acpi_find_bus_speed(struct device *dev)
298 struct i2c_acpi_lookup lookup;
299 struct i2c_board_info dummy;
302 if (!has_acpi_companion(dev))
305 memset(&lookup, 0, sizeof(lookup));
306 lookup.search_handle = ACPI_HANDLE(dev);
307 lookup.min_speed = UINT_MAX;
308 lookup.info = &dummy;
311 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
312 I2C_ACPI_MAX_SCAN_DEPTH,
313 i2c_acpi_lookup_speed, NULL,
316 if (ACPI_FAILURE(status)) {
317 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
321 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
323 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
325 static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
327 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
332 return ACPI_HANDLE(dev) == (acpi_handle)data;
335 static int i2c_acpi_find_match_device(struct device *dev, void *data)
337 return ACPI_COMPANION(dev) == data;
340 static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
344 dev = bus_find_device(&i2c_bus_type, NULL, handle,
345 i2c_acpi_find_match_adapter);
346 return dev ? i2c_verify_adapter(dev) : NULL;
349 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
353 dev = bus_find_device(&i2c_bus_type, NULL, adev,
354 i2c_acpi_find_match_device);
355 return dev ? i2c_verify_client(dev) : NULL;
358 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
361 struct acpi_device *adev = arg;
362 struct i2c_board_info info;
363 acpi_handle adapter_handle;
364 struct i2c_adapter *adapter;
365 struct i2c_client *client;
368 case ACPI_RECONFIG_DEVICE_ADD:
369 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
372 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
376 i2c_acpi_register_device(adapter, adev, &info);
378 case ACPI_RECONFIG_DEVICE_REMOVE:
379 if (!acpi_device_enumerated(adev))
382 client = i2c_acpi_find_client_by_adev(adev);
386 i2c_unregister_device(client);
387 put_device(&client->dev);
394 struct notifier_block i2c_acpi_notifier = {
395 .notifier_call = i2c_acpi_notify,
399 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
400 * @dev: Device owning the ACPI resources to get the client from
401 * @index: Index of ACPI resource to get
402 * @info: describes the I2C device; note this is modified (addr gets set)
405 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
406 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
407 * resources, in that case this function can be used to create an i2c-client
408 * for other I2cSerialBus resources in the Current Resource Settings table.
410 * Also see i2c_new_device, which this function calls to create the i2c-client.
412 * Returns a pointer to the new i2c-client, or error pointer in case of failure.
413 * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
415 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
416 struct i2c_board_info *info)
418 struct i2c_acpi_lookup lookup;
419 struct i2c_adapter *adapter;
420 struct i2c_client *client;
421 struct acpi_device *adev;
422 LIST_HEAD(resource_list);
425 adev = ACPI_COMPANION(dev);
427 return ERR_PTR(-EINVAL);
429 memset(&lookup, 0, sizeof(lookup));
431 lookup.device_handle = acpi_device_handle(adev);
432 lookup.index = index;
434 ret = acpi_dev_get_resources(adev, &resource_list,
435 i2c_acpi_fill_info, &lookup);
439 acpi_dev_free_resource_list(&resource_list);
442 return ERR_PTR(-EADDRNOTAVAIL);
444 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
446 return ERR_PTR(-EPROBE_DEFER);
448 client = i2c_new_device(adapter, info);
450 return ERR_PTR(-ENODEV);
454 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
456 #ifdef CONFIG_ACPI_I2C_OPREGION
457 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
458 u8 cmd, u8 *data, u8 data_len)
461 struct i2c_msg msgs[2];
465 buffer = kzalloc(data_len, GFP_KERNEL);
469 msgs[0].addr = client->addr;
470 msgs[0].flags = client->flags;
474 msgs[1].addr = client->addr;
475 msgs[1].flags = client->flags | I2C_M_RD;
476 msgs[1].len = data_len;
477 msgs[1].buf = buffer;
479 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
481 /* Getting a NACK is unfortunately normal with some DSTDs */
482 if (ret == -EREMOTEIO)
483 dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
484 data_len, client->addr, cmd, ret);
486 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
487 data_len, client->addr, cmd, ret);
488 /* 2 transfers must have completed successfully */
489 } else if (ret == 2) {
490 memcpy(data, buffer, data_len);
500 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
501 u8 cmd, u8 *data, u8 data_len)
504 struct i2c_msg msgs[1];
508 buffer = kzalloc(data_len + 1, GFP_KERNEL);
513 memcpy(buffer + 1, data, data_len);
515 msgs[0].addr = client->addr;
516 msgs[0].flags = client->flags;
517 msgs[0].len = data_len + 1;
518 msgs[0].buf = buffer;
520 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
525 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
529 /* 1 transfer must have completed successfully */
530 return (ret == 1) ? 0 : -EIO;
534 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
535 u32 bits, u64 *value64,
536 void *handler_context, void *region_context)
538 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
539 struct i2c_acpi_handler_data *data = handler_context;
540 struct acpi_connection_info *info = &data->info;
541 struct acpi_resource_i2c_serialbus *sb;
542 struct i2c_adapter *adapter = data->adapter;
543 struct i2c_client *client;
544 struct acpi_resource *ares;
545 u32 accessor_type = function >> 16;
546 u8 action = function & ACPI_IO_MASK;
550 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
551 if (ACPI_FAILURE(ret))
554 client = kzalloc(sizeof(*client), GFP_KERNEL);
560 if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
561 ret = AE_BAD_PARAMETER;
565 client->adapter = adapter;
566 client->addr = sb->slave_address;
568 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
569 client->flags |= I2C_CLIENT_TEN;
571 switch (accessor_type) {
572 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
573 if (action == ACPI_READ) {
574 status = i2c_smbus_read_byte(client);
580 status = i2c_smbus_write_byte(client, gsb->bdata);
584 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
585 if (action == ACPI_READ) {
586 status = i2c_smbus_read_byte_data(client, command);
592 status = i2c_smbus_write_byte_data(client, command,
597 case ACPI_GSB_ACCESS_ATTRIB_WORD:
598 if (action == ACPI_READ) {
599 status = i2c_smbus_read_word_data(client, command);
605 status = i2c_smbus_write_word_data(client, command,
610 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
611 if (action == ACPI_READ) {
612 status = i2c_smbus_read_block_data(client, command,
619 status = i2c_smbus_write_block_data(client, command,
620 gsb->len, gsb->data);
624 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
625 if (action == ACPI_READ) {
626 status = acpi_gsb_i2c_read_bytes(client, command,
627 gsb->data, info->access_length);
629 status = acpi_gsb_i2c_write_bytes(client, command,
630 gsb->data, info->access_length);
635 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
636 accessor_type, client->addr);
637 ret = AE_BAD_PARAMETER;
641 gsb->status = status;
650 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
653 struct i2c_acpi_handler_data *data;
656 if (!adapter->dev.parent)
659 handle = ACPI_HANDLE(adapter->dev.parent);
664 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
669 data->adapter = adapter;
670 status = acpi_bus_attach_private_data(handle, (void *)data);
671 if (ACPI_FAILURE(status)) {
676 status = acpi_install_address_space_handler(handle,
677 ACPI_ADR_SPACE_GSBUS,
678 &i2c_acpi_space_handler,
681 if (ACPI_FAILURE(status)) {
682 dev_err(&adapter->dev, "Error installing i2c space handler\n");
683 acpi_bus_detach_private_data(handle);
688 acpi_walk_dep_device_list(handle);
692 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
695 struct i2c_acpi_handler_data *data;
698 if (!adapter->dev.parent)
701 handle = ACPI_HANDLE(adapter->dev.parent);
706 acpi_remove_address_space_handler(handle,
707 ACPI_ADR_SPACE_GSBUS,
708 &i2c_acpi_space_handler);
710 status = acpi_bus_get_private_data(handle, (void **)&data);
711 if (ACPI_SUCCESS(status))
714 acpi_bus_detach_private_data(handle);
716 #endif /* CONFIG_ACPI_I2C_OPREGION */