xref: /linux/drivers/i2c/i2c-core-acpi.c (revision 90a3be9b194505165ecbbd9fe600818f5618dfbe)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
253f8f7c5SWolfram Sang /*
353f8f7c5SWolfram Sang  * Linux I2C core ACPI support code
453f8f7c5SWolfram Sang  *
553f8f7c5SWolfram Sang  * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
653f8f7c5SWolfram Sang  */
753f8f7c5SWolfram Sang 
853f8f7c5SWolfram Sang #include <linux/acpi.h>
953f8f7c5SWolfram Sang #include <linux/device.h>
1053f8f7c5SWolfram Sang #include <linux/err.h>
1153f8f7c5SWolfram Sang #include <linux/i2c.h>
1253f8f7c5SWolfram Sang #include <linux/list.h>
1353f8f7c5SWolfram Sang #include <linux/module.h>
1453f8f7c5SWolfram Sang #include <linux/slab.h>
1553f8f7c5SWolfram Sang 
1653f8f7c5SWolfram Sang #include "i2c-core.h"
1753f8f7c5SWolfram Sang 
1853f8f7c5SWolfram Sang struct i2c_acpi_handler_data {
1953f8f7c5SWolfram Sang 	struct acpi_connection_info info;
2053f8f7c5SWolfram Sang 	struct i2c_adapter *adapter;
2153f8f7c5SWolfram Sang };
2253f8f7c5SWolfram Sang 
2353f8f7c5SWolfram Sang struct gsb_buffer {
2453f8f7c5SWolfram Sang 	u8	status;
2553f8f7c5SWolfram Sang 	u8	len;
2653f8f7c5SWolfram Sang 	union {
2753f8f7c5SWolfram Sang 		u16	wdata;
2853f8f7c5SWolfram Sang 		u8	bdata;
2953f8f7c5SWolfram Sang 		u8	data[0];
3053f8f7c5SWolfram Sang 	};
3153f8f7c5SWolfram Sang } __packed;
3253f8f7c5SWolfram Sang 
3353f8f7c5SWolfram Sang struct i2c_acpi_lookup {
3453f8f7c5SWolfram Sang 	struct i2c_board_info *info;
3553f8f7c5SWolfram Sang 	acpi_handle adapter_handle;
3653f8f7c5SWolfram Sang 	acpi_handle device_handle;
3753f8f7c5SWolfram Sang 	acpi_handle search_handle;
3853f8f7c5SWolfram Sang 	int n;
3953f8f7c5SWolfram Sang 	int index;
4053f8f7c5SWolfram Sang 	u32 speed;
4153f8f7c5SWolfram Sang 	u32 min_speed;
427574c0dbSHans de Goede 	u32 force_speed;
4353f8f7c5SWolfram Sang };
4453f8f7c5SWolfram Sang 
450d5102feSAndy Shevchenko /**
460d5102feSAndy Shevchenko  * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
470d5102feSAndy Shevchenko  * @ares:	ACPI resource
480d5102feSAndy Shevchenko  * @i2c:	Pointer to I2cSerialBus resource will be returned here
490d5102feSAndy Shevchenko  *
500d5102feSAndy Shevchenko  * Checks if the given ACPI resource is of type I2cSerialBus.
510d5102feSAndy Shevchenko  * In this case, returns a pointer to it to the caller.
520d5102feSAndy Shevchenko  *
530d5102feSAndy Shevchenko  * Returns true if resource type is of I2cSerialBus, otherwise false.
540d5102feSAndy Shevchenko  */
550d5102feSAndy Shevchenko bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
560d5102feSAndy Shevchenko 			       struct acpi_resource_i2c_serialbus **i2c)
570d5102feSAndy Shevchenko {
580d5102feSAndy Shevchenko 	struct acpi_resource_i2c_serialbus *sb;
590d5102feSAndy Shevchenko 
600d5102feSAndy Shevchenko 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
610d5102feSAndy Shevchenko 		return false;
620d5102feSAndy Shevchenko 
630d5102feSAndy Shevchenko 	sb = &ares->data.i2c_serial_bus;
640d5102feSAndy Shevchenko 	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
650d5102feSAndy Shevchenko 		return false;
660d5102feSAndy Shevchenko 
670d5102feSAndy Shevchenko 	*i2c = sb;
680d5102feSAndy Shevchenko 	return true;
690d5102feSAndy Shevchenko }
700d5102feSAndy Shevchenko EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
710d5102feSAndy Shevchenko 
7253f8f7c5SWolfram Sang static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
7353f8f7c5SWolfram Sang {
7453f8f7c5SWolfram Sang 	struct i2c_acpi_lookup *lookup = data;
7553f8f7c5SWolfram Sang 	struct i2c_board_info *info = lookup->info;
7653f8f7c5SWolfram Sang 	struct acpi_resource_i2c_serialbus *sb;
7753f8f7c5SWolfram Sang 	acpi_status status;
7853f8f7c5SWolfram Sang 
790d5102feSAndy Shevchenko 	if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
8053f8f7c5SWolfram Sang 		return 1;
8153f8f7c5SWolfram Sang 
8253f8f7c5SWolfram Sang 	if (lookup->index != -1 && lookup->n++ != lookup->index)
8353f8f7c5SWolfram Sang 		return 1;
8453f8f7c5SWolfram Sang 
8553f8f7c5SWolfram Sang 	status = acpi_get_handle(lookup->device_handle,
8653f8f7c5SWolfram Sang 				 sb->resource_source.string_ptr,
8753f8f7c5SWolfram Sang 				 &lookup->adapter_handle);
885f59d6a1SAndy Shevchenko 	if (ACPI_FAILURE(status))
8953f8f7c5SWolfram Sang 		return 1;
9053f8f7c5SWolfram Sang 
9153f8f7c5SWolfram Sang 	info->addr = sb->slave_address;
9253f8f7c5SWolfram Sang 	lookup->speed = sb->connection_speed;
9353f8f7c5SWolfram Sang 	if (sb->access_mode == ACPI_I2C_10BIT_MODE)
9453f8f7c5SWolfram Sang 		info->flags |= I2C_CLIENT_TEN;
9553f8f7c5SWolfram Sang 
9653f8f7c5SWolfram Sang 	return 1;
9753f8f7c5SWolfram Sang }
9853f8f7c5SWolfram Sang 
993a4991a9SHans de Goede static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
1003a4991a9SHans de Goede 	/*
1013a4991a9SHans de Goede 	 * ACPI video acpi_devices, which are handled by the acpi-video driver
1023a4991a9SHans de Goede 	 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
1033a4991a9SHans de Goede 	 */
1043a4991a9SHans de Goede 	{ ACPI_VIDEO_HID, 0 },
1053a4991a9SHans de Goede 	{}
1063a4991a9SHans de Goede };
1073a4991a9SHans de Goede 
10853f8f7c5SWolfram Sang static int i2c_acpi_do_lookup(struct acpi_device *adev,
10953f8f7c5SWolfram Sang 			      struct i2c_acpi_lookup *lookup)
11053f8f7c5SWolfram Sang {
11153f8f7c5SWolfram Sang 	struct i2c_board_info *info = lookup->info;
11253f8f7c5SWolfram Sang 	struct list_head resource_list;
11353f8f7c5SWolfram Sang 	int ret;
11453f8f7c5SWolfram Sang 
1154befedc0SArd Biesheuvel 	if (acpi_bus_get_status(adev) || !adev->status.present)
11653f8f7c5SWolfram Sang 		return -EINVAL;
11753f8f7c5SWolfram Sang 
1183a4991a9SHans de Goede 	if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
1193a4991a9SHans de Goede 		return -ENODEV;
1203a4991a9SHans de Goede 
12153f8f7c5SWolfram Sang 	memset(info, 0, sizeof(*info));
12253f8f7c5SWolfram Sang 	lookup->device_handle = acpi_device_handle(adev);
12353f8f7c5SWolfram Sang 
12453f8f7c5SWolfram Sang 	/* Look up for I2cSerialBus resource */
12553f8f7c5SWolfram Sang 	INIT_LIST_HEAD(&resource_list);
12653f8f7c5SWolfram Sang 	ret = acpi_dev_get_resources(adev, &resource_list,
12753f8f7c5SWolfram Sang 				     i2c_acpi_fill_info, lookup);
12853f8f7c5SWolfram Sang 	acpi_dev_free_resource_list(&resource_list);
12953f8f7c5SWolfram Sang 
13053f8f7c5SWolfram Sang 	if (ret < 0 || !info->addr)
13153f8f7c5SWolfram Sang 		return -EINVAL;
13253f8f7c5SWolfram Sang 
13353f8f7c5SWolfram Sang 	return 0;
13453f8f7c5SWolfram Sang }
13553f8f7c5SWolfram Sang 
136c2223ddcSCharles Keepax static int i2c_acpi_add_resource(struct acpi_resource *ares, void *data)
137c2223ddcSCharles Keepax {
138c2223ddcSCharles Keepax 	int *irq = data;
139c2223ddcSCharles Keepax 	struct resource r;
140c2223ddcSCharles Keepax 
141c2223ddcSCharles Keepax 	if (*irq <= 0 && acpi_dev_resource_interrupt(ares, 0, &r))
142c2223ddcSCharles Keepax 		*irq = i2c_dev_irq_from_resources(&r, 1);
143c2223ddcSCharles Keepax 
144c2223ddcSCharles Keepax 	return 1; /* No need to add resource to the list */
145c2223ddcSCharles Keepax }
146c2223ddcSCharles Keepax 
14716c9db1dSCharles Keepax /**
14816c9db1dSCharles Keepax  * i2c_acpi_get_irq - get device IRQ number from ACPI
14916c9db1dSCharles Keepax  * @client: Pointer to the I2C client device
15016c9db1dSCharles Keepax  *
15116c9db1dSCharles Keepax  * Find the IRQ number used by a specific client device.
15216c9db1dSCharles Keepax  *
15316c9db1dSCharles Keepax  * Return: The IRQ number or an error code.
15416c9db1dSCharles Keepax  */
15516c9db1dSCharles Keepax int i2c_acpi_get_irq(struct i2c_client *client)
156a52e3b37SCharles Keepax {
15716c9db1dSCharles Keepax 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
158a52e3b37SCharles Keepax 	struct list_head resource_list;
159a52e3b37SCharles Keepax 	int irq = -ENOENT;
160a52e3b37SCharles Keepax 	int ret;
161a52e3b37SCharles Keepax 
162a52e3b37SCharles Keepax 	INIT_LIST_HEAD(&resource_list);
163a52e3b37SCharles Keepax 
164a52e3b37SCharles Keepax 	ret = acpi_dev_get_resources(adev, &resource_list,
165a52e3b37SCharles Keepax 				     i2c_acpi_add_resource, &irq);
166a52e3b37SCharles Keepax 	if (ret < 0)
167a52e3b37SCharles Keepax 		return ret;
168a52e3b37SCharles Keepax 
169a52e3b37SCharles Keepax 	acpi_dev_free_resource_list(&resource_list);
170a52e3b37SCharles Keepax 
1718466b616SCharles Keepax 	if (irq == -ENOENT)
1728466b616SCharles Keepax 		irq = acpi_dev_gpio_irq_get(adev, 0);
1738466b616SCharles Keepax 
174a52e3b37SCharles Keepax 	return irq;
175a52e3b37SCharles Keepax }
176a52e3b37SCharles Keepax 
17753f8f7c5SWolfram Sang static int i2c_acpi_get_info(struct acpi_device *adev,
17853f8f7c5SWolfram Sang 			     struct i2c_board_info *info,
17953f8f7c5SWolfram Sang 			     struct i2c_adapter *adapter,
18053f8f7c5SWolfram Sang 			     acpi_handle *adapter_handle)
18153f8f7c5SWolfram Sang {
18253f8f7c5SWolfram Sang 	struct i2c_acpi_lookup lookup;
18353f8f7c5SWolfram Sang 	int ret;
18453f8f7c5SWolfram Sang 
18553f8f7c5SWolfram Sang 	memset(&lookup, 0, sizeof(lookup));
18653f8f7c5SWolfram Sang 	lookup.info = info;
18753f8f7c5SWolfram Sang 	lookup.index = -1;
18853f8f7c5SWolfram Sang 
1894befedc0SArd Biesheuvel 	if (acpi_device_enumerated(adev))
1904befedc0SArd Biesheuvel 		return -EINVAL;
1914befedc0SArd Biesheuvel 
19253f8f7c5SWolfram Sang 	ret = i2c_acpi_do_lookup(adev, &lookup);
19353f8f7c5SWolfram Sang 	if (ret)
19453f8f7c5SWolfram Sang 		return ret;
19553f8f7c5SWolfram Sang 
19653f8f7c5SWolfram Sang 	if (adapter) {
19753f8f7c5SWolfram Sang 		/* The adapter must match the one in I2cSerialBus() connector */
19853f8f7c5SWolfram Sang 		if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
19953f8f7c5SWolfram Sang 			return -ENODEV;
20053f8f7c5SWolfram Sang 	} else {
20153f8f7c5SWolfram Sang 		struct acpi_device *adapter_adev;
20253f8f7c5SWolfram Sang 
20353f8f7c5SWolfram Sang 		/* The adapter must be present */
20453f8f7c5SWolfram Sang 		if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
20553f8f7c5SWolfram Sang 			return -ENODEV;
20653f8f7c5SWolfram Sang 		if (acpi_bus_get_status(adapter_adev) ||
20753f8f7c5SWolfram Sang 		    !adapter_adev->status.present)
20853f8f7c5SWolfram Sang 			return -ENODEV;
20953f8f7c5SWolfram Sang 	}
21053f8f7c5SWolfram Sang 
21153f8f7c5SWolfram Sang 	info->fwnode = acpi_fwnode_handle(adev);
21253f8f7c5SWolfram Sang 	if (adapter_handle)
21353f8f7c5SWolfram Sang 		*adapter_handle = lookup.adapter_handle;
21453f8f7c5SWolfram Sang 
21553f8f7c5SWolfram Sang 	acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
21653f8f7c5SWolfram Sang 			  sizeof(info->type));
21753f8f7c5SWolfram Sang 
21853f8f7c5SWolfram Sang 	return 0;
21953f8f7c5SWolfram Sang }
22053f8f7c5SWolfram Sang 
22153f8f7c5SWolfram Sang static void i2c_acpi_register_device(struct i2c_adapter *adapter,
22253f8f7c5SWolfram Sang 				     struct acpi_device *adev,
22353f8f7c5SWolfram Sang 				     struct i2c_board_info *info)
22453f8f7c5SWolfram Sang {
22553f8f7c5SWolfram Sang 	adev->power.flags.ignore_parent = true;
22653f8f7c5SWolfram Sang 	acpi_device_set_enumerated(adev);
22753f8f7c5SWolfram Sang 
228*90a3be9bSWolfram Sang 	if (IS_ERR(i2c_new_client_device(adapter, info))) {
22953f8f7c5SWolfram Sang 		adev->power.flags.ignore_parent = false;
23053f8f7c5SWolfram Sang 		dev_err(&adapter->dev,
23153f8f7c5SWolfram Sang 			"failed to add I2C device %s from ACPI\n",
23253f8f7c5SWolfram Sang 			dev_name(&adev->dev));
23353f8f7c5SWolfram Sang 	}
23453f8f7c5SWolfram Sang }
23553f8f7c5SWolfram Sang 
23653f8f7c5SWolfram Sang static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
23753f8f7c5SWolfram Sang 				       void *data, void **return_value)
23853f8f7c5SWolfram Sang {
23953f8f7c5SWolfram Sang 	struct i2c_adapter *adapter = data;
24053f8f7c5SWolfram Sang 	struct acpi_device *adev;
24153f8f7c5SWolfram Sang 	struct i2c_board_info info;
24253f8f7c5SWolfram Sang 
24353f8f7c5SWolfram Sang 	if (acpi_bus_get_device(handle, &adev))
24453f8f7c5SWolfram Sang 		return AE_OK;
24553f8f7c5SWolfram Sang 
24653f8f7c5SWolfram Sang 	if (i2c_acpi_get_info(adev, &info, adapter, NULL))
24753f8f7c5SWolfram Sang 		return AE_OK;
24853f8f7c5SWolfram Sang 
24953f8f7c5SWolfram Sang 	i2c_acpi_register_device(adapter, adev, &info);
25053f8f7c5SWolfram Sang 
25153f8f7c5SWolfram Sang 	return AE_OK;
25253f8f7c5SWolfram Sang }
25353f8f7c5SWolfram Sang 
25453f8f7c5SWolfram Sang #define I2C_ACPI_MAX_SCAN_DEPTH 32
25553f8f7c5SWolfram Sang 
25653f8f7c5SWolfram Sang /**
25753f8f7c5SWolfram Sang  * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
25853f8f7c5SWolfram Sang  * @adap: pointer to adapter
25953f8f7c5SWolfram Sang  *
26053f8f7c5SWolfram Sang  * Enumerate all I2C slave devices behind this adapter by walking the ACPI
26153f8f7c5SWolfram Sang  * namespace. When a device is found it will be added to the Linux device
26253f8f7c5SWolfram Sang  * model and bound to the corresponding ACPI handle.
26353f8f7c5SWolfram Sang  */
26453f8f7c5SWolfram Sang void i2c_acpi_register_devices(struct i2c_adapter *adap)
26553f8f7c5SWolfram Sang {
26653f8f7c5SWolfram Sang 	acpi_status status;
26753f8f7c5SWolfram Sang 
26853f8f7c5SWolfram Sang 	if (!has_acpi_companion(&adap->dev))
26953f8f7c5SWolfram Sang 		return;
27053f8f7c5SWolfram Sang 
27153f8f7c5SWolfram Sang 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
27253f8f7c5SWolfram Sang 				     I2C_ACPI_MAX_SCAN_DEPTH,
27353f8f7c5SWolfram Sang 				     i2c_acpi_add_device, NULL,
27453f8f7c5SWolfram Sang 				     adap, NULL);
27553f8f7c5SWolfram Sang 	if (ACPI_FAILURE(status))
27653f8f7c5SWolfram Sang 		dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
27753f8f7c5SWolfram Sang }
27853f8f7c5SWolfram Sang 
279c64ffff7SAndy Shevchenko const struct acpi_device_id *
280c64ffff7SAndy Shevchenko i2c_acpi_match_device(const struct acpi_device_id *matches,
281c64ffff7SAndy Shevchenko 		      struct i2c_client *client)
282c64ffff7SAndy Shevchenko {
283c64ffff7SAndy Shevchenko 	if (!(client && matches))
284c64ffff7SAndy Shevchenko 		return NULL;
285c64ffff7SAndy Shevchenko 
286c64ffff7SAndy Shevchenko 	return acpi_match_device(matches, &client->dev);
287c64ffff7SAndy Shevchenko }
288c64ffff7SAndy Shevchenko 
2897574c0dbSHans de Goede static const struct acpi_device_id i2c_acpi_force_400khz_device_ids[] = {
2907574c0dbSHans de Goede 	/*
2917574c0dbSHans de Goede 	 * These Silead touchscreen controllers only work at 400KHz, for
2927574c0dbSHans de Goede 	 * some reason they do not work at 100KHz. On some devices the ACPI
2937574c0dbSHans de Goede 	 * tables list another device at their bus as only being capable
2947574c0dbSHans de Goede 	 * of 100KHz, testing has shown that these other devices work fine
2957574c0dbSHans de Goede 	 * at 400KHz (as can be expected of any recent i2c hw) so we force
2967574c0dbSHans de Goede 	 * the speed of the bus to 400 KHz if a Silead device is present.
2977574c0dbSHans de Goede 	 */
2987574c0dbSHans de Goede 	{ "MSSL1680", 0 },
2997574c0dbSHans de Goede 	{}
3007574c0dbSHans de Goede };
3017574c0dbSHans de Goede 
30253f8f7c5SWolfram Sang static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
30353f8f7c5SWolfram Sang 					   void *data, void **return_value)
30453f8f7c5SWolfram Sang {
30553f8f7c5SWolfram Sang 	struct i2c_acpi_lookup *lookup = data;
30653f8f7c5SWolfram Sang 	struct acpi_device *adev;
30753f8f7c5SWolfram Sang 
30853f8f7c5SWolfram Sang 	if (acpi_bus_get_device(handle, &adev))
30953f8f7c5SWolfram Sang 		return AE_OK;
31053f8f7c5SWolfram Sang 
31153f8f7c5SWolfram Sang 	if (i2c_acpi_do_lookup(adev, lookup))
31253f8f7c5SWolfram Sang 		return AE_OK;
31353f8f7c5SWolfram Sang 
31453f8f7c5SWolfram Sang 	if (lookup->search_handle != lookup->adapter_handle)
31553f8f7c5SWolfram Sang 		return AE_OK;
31653f8f7c5SWolfram Sang 
31753f8f7c5SWolfram Sang 	if (lookup->speed <= lookup->min_speed)
31853f8f7c5SWolfram Sang 		lookup->min_speed = lookup->speed;
31953f8f7c5SWolfram Sang 
3207574c0dbSHans de Goede 	if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
3217574c0dbSHans de Goede 		lookup->force_speed = 400000;
3227574c0dbSHans de Goede 
32353f8f7c5SWolfram Sang 	return AE_OK;
32453f8f7c5SWolfram Sang }
32553f8f7c5SWolfram Sang 
32653f8f7c5SWolfram Sang /**
32753f8f7c5SWolfram Sang  * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
32853f8f7c5SWolfram Sang  * @dev: The device owning the bus
32953f8f7c5SWolfram Sang  *
33053f8f7c5SWolfram Sang  * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
33153f8f7c5SWolfram Sang  * devices connected to this bus and use the speed of slowest device.
33253f8f7c5SWolfram Sang  *
33353f8f7c5SWolfram Sang  * Returns the speed in Hz or zero
33453f8f7c5SWolfram Sang  */
33553f8f7c5SWolfram Sang u32 i2c_acpi_find_bus_speed(struct device *dev)
33653f8f7c5SWolfram Sang {
33753f8f7c5SWolfram Sang 	struct i2c_acpi_lookup lookup;
33853f8f7c5SWolfram Sang 	struct i2c_board_info dummy;
33953f8f7c5SWolfram Sang 	acpi_status status;
34053f8f7c5SWolfram Sang 
34153f8f7c5SWolfram Sang 	if (!has_acpi_companion(dev))
34253f8f7c5SWolfram Sang 		return 0;
34353f8f7c5SWolfram Sang 
34453f8f7c5SWolfram Sang 	memset(&lookup, 0, sizeof(lookup));
34553f8f7c5SWolfram Sang 	lookup.search_handle = ACPI_HANDLE(dev);
34653f8f7c5SWolfram Sang 	lookup.min_speed = UINT_MAX;
34753f8f7c5SWolfram Sang 	lookup.info = &dummy;
34853f8f7c5SWolfram Sang 	lookup.index = -1;
34953f8f7c5SWolfram Sang 
35053f8f7c5SWolfram Sang 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
35153f8f7c5SWolfram Sang 				     I2C_ACPI_MAX_SCAN_DEPTH,
35253f8f7c5SWolfram Sang 				     i2c_acpi_lookup_speed, NULL,
35353f8f7c5SWolfram Sang 				     &lookup, NULL);
35453f8f7c5SWolfram Sang 
35553f8f7c5SWolfram Sang 	if (ACPI_FAILURE(status)) {
35653f8f7c5SWolfram Sang 		dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
35753f8f7c5SWolfram Sang 		return 0;
35853f8f7c5SWolfram Sang 	}
35953f8f7c5SWolfram Sang 
3607574c0dbSHans de Goede 	if (lookup.force_speed) {
3617574c0dbSHans de Goede 		if (lookup.force_speed != lookup.min_speed)
3627574c0dbSHans de Goede 			dev_warn(dev, FW_BUG "DSDT uses known not-working I2C bus speed %d, forcing it to %d\n",
3637574c0dbSHans de Goede 				 lookup.min_speed, lookup.force_speed);
3647574c0dbSHans de Goede 		return lookup.force_speed;
3657574c0dbSHans de Goede 	} else if (lookup.min_speed != UINT_MAX) {
3667574c0dbSHans de Goede 		return lookup.min_speed;
3677574c0dbSHans de Goede 	} else {
3687574c0dbSHans de Goede 		return 0;
3697574c0dbSHans de Goede 	}
37053f8f7c5SWolfram Sang }
37153f8f7c5SWolfram Sang EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
37253f8f7c5SWolfram Sang 
373418e3ea1SSuzuki K Poulose static int i2c_acpi_find_match_adapter(struct device *dev, const void *data)
37453f8f7c5SWolfram Sang {
37553f8f7c5SWolfram Sang 	struct i2c_adapter *adapter = i2c_verify_adapter(dev);
37653f8f7c5SWolfram Sang 
37753f8f7c5SWolfram Sang 	if (!adapter)
37853f8f7c5SWolfram Sang 		return 0;
37953f8f7c5SWolfram Sang 
38053f8f7c5SWolfram Sang 	return ACPI_HANDLE(dev) == (acpi_handle)data;
38153f8f7c5SWolfram Sang }
38253f8f7c5SWolfram Sang 
3831e91a2e5SRuslan Babayev struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
38453f8f7c5SWolfram Sang {
385644bf600SSuzuki K Poulose 	struct device *dev;
386644bf600SSuzuki K Poulose 
387644bf600SSuzuki K Poulose 	dev = bus_find_device(&i2c_bus_type, NULL, handle,
388644bf600SSuzuki K Poulose 			      i2c_acpi_find_match_adapter);
38953f8f7c5SWolfram Sang 
39053f8f7c5SWolfram Sang 	return dev ? i2c_verify_adapter(dev) : NULL;
39153f8f7c5SWolfram Sang }
3921e91a2e5SRuslan Babayev EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
39353f8f7c5SWolfram Sang 
39453f8f7c5SWolfram Sang static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
39553f8f7c5SWolfram Sang {
39653f8f7c5SWolfram Sang 	struct device *dev;
39753f8f7c5SWolfram Sang 
39800500147SSuzuki K Poulose 	dev = bus_find_device_by_acpi_dev(&i2c_bus_type, adev);
39953f8f7c5SWolfram Sang 	return dev ? i2c_verify_client(dev) : NULL;
40053f8f7c5SWolfram Sang }
40153f8f7c5SWolfram Sang 
40253f8f7c5SWolfram Sang static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
40353f8f7c5SWolfram Sang 			   void *arg)
40453f8f7c5SWolfram Sang {
40553f8f7c5SWolfram Sang 	struct acpi_device *adev = arg;
40653f8f7c5SWolfram Sang 	struct i2c_board_info info;
40753f8f7c5SWolfram Sang 	acpi_handle adapter_handle;
40853f8f7c5SWolfram Sang 	struct i2c_adapter *adapter;
40953f8f7c5SWolfram Sang 	struct i2c_client *client;
41053f8f7c5SWolfram Sang 
41153f8f7c5SWolfram Sang 	switch (value) {
41253f8f7c5SWolfram Sang 	case ACPI_RECONFIG_DEVICE_ADD:
41353f8f7c5SWolfram Sang 		if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
41453f8f7c5SWolfram Sang 			break;
41553f8f7c5SWolfram Sang 
41653f8f7c5SWolfram Sang 		adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
41753f8f7c5SWolfram Sang 		if (!adapter)
41853f8f7c5SWolfram Sang 			break;
41953f8f7c5SWolfram Sang 
42053f8f7c5SWolfram Sang 		i2c_acpi_register_device(adapter, adev, &info);
42153f8f7c5SWolfram Sang 		break;
42253f8f7c5SWolfram Sang 	case ACPI_RECONFIG_DEVICE_REMOVE:
42353f8f7c5SWolfram Sang 		if (!acpi_device_enumerated(adev))
42453f8f7c5SWolfram Sang 			break;
42553f8f7c5SWolfram Sang 
42653f8f7c5SWolfram Sang 		client = i2c_acpi_find_client_by_adev(adev);
42753f8f7c5SWolfram Sang 		if (!client)
42853f8f7c5SWolfram Sang 			break;
42953f8f7c5SWolfram Sang 
43053f8f7c5SWolfram Sang 		i2c_unregister_device(client);
43153f8f7c5SWolfram Sang 		put_device(&client->dev);
43253f8f7c5SWolfram Sang 		break;
43353f8f7c5SWolfram Sang 	}
43453f8f7c5SWolfram Sang 
43553f8f7c5SWolfram Sang 	return NOTIFY_OK;
43653f8f7c5SWolfram Sang }
43753f8f7c5SWolfram Sang 
43853f8f7c5SWolfram Sang struct notifier_block i2c_acpi_notifier = {
43953f8f7c5SWolfram Sang 	.notifier_call = i2c_acpi_notify,
44053f8f7c5SWolfram Sang };
44153f8f7c5SWolfram Sang 
44253f8f7c5SWolfram Sang /**
44353f8f7c5SWolfram Sang  * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
44453f8f7c5SWolfram Sang  * @dev:     Device owning the ACPI resources to get the client from
44553f8f7c5SWolfram Sang  * @index:   Index of ACPI resource to get
44653f8f7c5SWolfram Sang  * @info:    describes the I2C device; note this is modified (addr gets set)
44753f8f7c5SWolfram Sang  * Context: can sleep
44853f8f7c5SWolfram Sang  *
44953f8f7c5SWolfram Sang  * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
45053f8f7c5SWolfram Sang  * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
45153f8f7c5SWolfram Sang  * resources, in that case this function can be used to create an i2c-client
45253f8f7c5SWolfram Sang  * for other I2cSerialBus resources in the Current Resource Settings table.
45353f8f7c5SWolfram Sang  *
454*90a3be9bSWolfram Sang  * Also see i2c_new_client_device, which this function calls to create the
455*90a3be9bSWolfram Sang  * i2c-client.
45653f8f7c5SWolfram Sang  *
4572dea645fSAndy Shevchenko  * Returns a pointer to the new i2c-client, or error pointer in case of failure.
4582dea645fSAndy Shevchenko  * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
45953f8f7c5SWolfram Sang  */
46053f8f7c5SWolfram Sang struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
46153f8f7c5SWolfram Sang 				       struct i2c_board_info *info)
46253f8f7c5SWolfram Sang {
46353f8f7c5SWolfram Sang 	struct i2c_acpi_lookup lookup;
46453f8f7c5SWolfram Sang 	struct i2c_adapter *adapter;
46553f8f7c5SWolfram Sang 	struct acpi_device *adev;
46653f8f7c5SWolfram Sang 	LIST_HEAD(resource_list);
46753f8f7c5SWolfram Sang 	int ret;
46853f8f7c5SWolfram Sang 
46953f8f7c5SWolfram Sang 	adev = ACPI_COMPANION(dev);
47053f8f7c5SWolfram Sang 	if (!adev)
4712dea645fSAndy Shevchenko 		return ERR_PTR(-EINVAL);
47253f8f7c5SWolfram Sang 
47353f8f7c5SWolfram Sang 	memset(&lookup, 0, sizeof(lookup));
47453f8f7c5SWolfram Sang 	lookup.info = info;
47553f8f7c5SWolfram Sang 	lookup.device_handle = acpi_device_handle(adev);
47653f8f7c5SWolfram Sang 	lookup.index = index;
47753f8f7c5SWolfram Sang 
47853f8f7c5SWolfram Sang 	ret = acpi_dev_get_resources(adev, &resource_list,
47953f8f7c5SWolfram Sang 				     i2c_acpi_fill_info, &lookup);
4802dea645fSAndy Shevchenko 	if (ret < 0)
4812dea645fSAndy Shevchenko 		return ERR_PTR(ret);
4822dea645fSAndy Shevchenko 
48353f8f7c5SWolfram Sang 	acpi_dev_free_resource_list(&resource_list);
48453f8f7c5SWolfram Sang 
4852dea645fSAndy Shevchenko 	if (!info->addr)
4862dea645fSAndy Shevchenko 		return ERR_PTR(-EADDRNOTAVAIL);
48753f8f7c5SWolfram Sang 
48853f8f7c5SWolfram Sang 	adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
48953f8f7c5SWolfram Sang 	if (!adapter)
4902dea645fSAndy Shevchenko 		return ERR_PTR(-EPROBE_DEFER);
49153f8f7c5SWolfram Sang 
492*90a3be9bSWolfram Sang 	return i2c_new_client_device(adapter, info);
49353f8f7c5SWolfram Sang }
49453f8f7c5SWolfram Sang EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
49553f8f7c5SWolfram Sang 
49653f8f7c5SWolfram Sang #ifdef CONFIG_ACPI_I2C_OPREGION
49753f8f7c5SWolfram Sang static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
49853f8f7c5SWolfram Sang 		u8 cmd, u8 *data, u8 data_len)
49953f8f7c5SWolfram Sang {
50053f8f7c5SWolfram Sang 
50153f8f7c5SWolfram Sang 	struct i2c_msg msgs[2];
50253f8f7c5SWolfram Sang 	int ret;
50353f8f7c5SWolfram Sang 	u8 *buffer;
50453f8f7c5SWolfram Sang 
50553f8f7c5SWolfram Sang 	buffer = kzalloc(data_len, GFP_KERNEL);
50653f8f7c5SWolfram Sang 	if (!buffer)
50753f8f7c5SWolfram Sang 		return AE_NO_MEMORY;
50853f8f7c5SWolfram Sang 
50953f8f7c5SWolfram Sang 	msgs[0].addr = client->addr;
51053f8f7c5SWolfram Sang 	msgs[0].flags = client->flags;
51153f8f7c5SWolfram Sang 	msgs[0].len = 1;
51253f8f7c5SWolfram Sang 	msgs[0].buf = &cmd;
51353f8f7c5SWolfram Sang 
51453f8f7c5SWolfram Sang 	msgs[1].addr = client->addr;
51553f8f7c5SWolfram Sang 	msgs[1].flags = client->flags | I2C_M_RD;
51653f8f7c5SWolfram Sang 	msgs[1].len = data_len;
51753f8f7c5SWolfram Sang 	msgs[1].buf = buffer;
51853f8f7c5SWolfram Sang 
51953f8f7c5SWolfram Sang 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
5207781edaeSHans de Goede 	if (ret < 0) {
5217781edaeSHans de Goede 		/* Getting a NACK is unfortunately normal with some DSTDs */
5227781edaeSHans de Goede 		if (ret == -EREMOTEIO)
5237781edaeSHans de Goede 			dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
524c8016fa2SHans de Goede 				data_len, client->addr, cmd, ret);
52553f8f7c5SWolfram Sang 		else
5267781edaeSHans de Goede 			dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
5277781edaeSHans de Goede 				data_len, client->addr, cmd, ret);
5280a30446cSHans de Goede 	/* 2 transfers must have completed successfully */
5290a30446cSHans de Goede 	} else if (ret == 2) {
53053f8f7c5SWolfram Sang 		memcpy(data, buffer, data_len);
5310a30446cSHans de Goede 		ret = 0;
5320a30446cSHans de Goede 	} else {
5330a30446cSHans de Goede 		ret = -EIO;
5347781edaeSHans de Goede 	}
53553f8f7c5SWolfram Sang 
53653f8f7c5SWolfram Sang 	kfree(buffer);
53753f8f7c5SWolfram Sang 	return ret;
53853f8f7c5SWolfram Sang }
53953f8f7c5SWolfram Sang 
54053f8f7c5SWolfram Sang static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
54153f8f7c5SWolfram Sang 		u8 cmd, u8 *data, u8 data_len)
54253f8f7c5SWolfram Sang {
54353f8f7c5SWolfram Sang 
54453f8f7c5SWolfram Sang 	struct i2c_msg msgs[1];
54553f8f7c5SWolfram Sang 	u8 *buffer;
54653f8f7c5SWolfram Sang 	int ret = AE_OK;
54753f8f7c5SWolfram Sang 
54853f8f7c5SWolfram Sang 	buffer = kzalloc(data_len + 1, GFP_KERNEL);
54953f8f7c5SWolfram Sang 	if (!buffer)
55053f8f7c5SWolfram Sang 		return AE_NO_MEMORY;
55153f8f7c5SWolfram Sang 
55253f8f7c5SWolfram Sang 	buffer[0] = cmd;
55353f8f7c5SWolfram Sang 	memcpy(buffer + 1, data, data_len);
55453f8f7c5SWolfram Sang 
55553f8f7c5SWolfram Sang 	msgs[0].addr = client->addr;
55653f8f7c5SWolfram Sang 	msgs[0].flags = client->flags;
55753f8f7c5SWolfram Sang 	msgs[0].len = data_len + 1;
55853f8f7c5SWolfram Sang 	msgs[0].buf = buffer;
55953f8f7c5SWolfram Sang 
56053f8f7c5SWolfram Sang 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
56153f8f7c5SWolfram Sang 
56253f8f7c5SWolfram Sang 	kfree(buffer);
563c463a158SHans de Goede 
564c463a158SHans de Goede 	if (ret < 0) {
565c463a158SHans de Goede 		dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
56653f8f7c5SWolfram Sang 		return ret;
56753f8f7c5SWolfram Sang 	}
56853f8f7c5SWolfram Sang 
569c463a158SHans de Goede 	/* 1 transfer must have completed successfully */
570c463a158SHans de Goede 	return (ret == 1) ? 0 : -EIO;
571c463a158SHans de Goede }
572c463a158SHans de Goede 
57353f8f7c5SWolfram Sang static acpi_status
57453f8f7c5SWolfram Sang i2c_acpi_space_handler(u32 function, acpi_physical_address command,
57553f8f7c5SWolfram Sang 			u32 bits, u64 *value64,
57653f8f7c5SWolfram Sang 			void *handler_context, void *region_context)
57753f8f7c5SWolfram Sang {
57853f8f7c5SWolfram Sang 	struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
57953f8f7c5SWolfram Sang 	struct i2c_acpi_handler_data *data = handler_context;
58053f8f7c5SWolfram Sang 	struct acpi_connection_info *info = &data->info;
58153f8f7c5SWolfram Sang 	struct acpi_resource_i2c_serialbus *sb;
58253f8f7c5SWolfram Sang 	struct i2c_adapter *adapter = data->adapter;
58353f8f7c5SWolfram Sang 	struct i2c_client *client;
58453f8f7c5SWolfram Sang 	struct acpi_resource *ares;
58553f8f7c5SWolfram Sang 	u32 accessor_type = function >> 16;
58653f8f7c5SWolfram Sang 	u8 action = function & ACPI_IO_MASK;
58753f8f7c5SWolfram Sang 	acpi_status ret;
58853f8f7c5SWolfram Sang 	int status;
58953f8f7c5SWolfram Sang 
59053f8f7c5SWolfram Sang 	ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
59153f8f7c5SWolfram Sang 	if (ACPI_FAILURE(ret))
59253f8f7c5SWolfram Sang 		return ret;
59353f8f7c5SWolfram Sang 
59453f8f7c5SWolfram Sang 	client = kzalloc(sizeof(*client), GFP_KERNEL);
59553f8f7c5SWolfram Sang 	if (!client) {
59653f8f7c5SWolfram Sang 		ret = AE_NO_MEMORY;
59753f8f7c5SWolfram Sang 		goto err;
59853f8f7c5SWolfram Sang 	}
59953f8f7c5SWolfram Sang 
6000d5102feSAndy Shevchenko 	if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
60153f8f7c5SWolfram Sang 		ret = AE_BAD_PARAMETER;
60253f8f7c5SWolfram Sang 		goto err;
60353f8f7c5SWolfram Sang 	}
60453f8f7c5SWolfram Sang 
60553f8f7c5SWolfram Sang 	client->adapter = adapter;
60653f8f7c5SWolfram Sang 	client->addr = sb->slave_address;
60753f8f7c5SWolfram Sang 
60853f8f7c5SWolfram Sang 	if (sb->access_mode == ACPI_I2C_10BIT_MODE)
60953f8f7c5SWolfram Sang 		client->flags |= I2C_CLIENT_TEN;
61053f8f7c5SWolfram Sang 
61153f8f7c5SWolfram Sang 	switch (accessor_type) {
61253f8f7c5SWolfram Sang 	case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
61353f8f7c5SWolfram Sang 		if (action == ACPI_READ) {
61453f8f7c5SWolfram Sang 			status = i2c_smbus_read_byte(client);
61553f8f7c5SWolfram Sang 			if (status >= 0) {
61653f8f7c5SWolfram Sang 				gsb->bdata = status;
61753f8f7c5SWolfram Sang 				status = 0;
61853f8f7c5SWolfram Sang 			}
61953f8f7c5SWolfram Sang 		} else {
62053f8f7c5SWolfram Sang 			status = i2c_smbus_write_byte(client, gsb->bdata);
62153f8f7c5SWolfram Sang 		}
62253f8f7c5SWolfram Sang 		break;
62353f8f7c5SWolfram Sang 
62453f8f7c5SWolfram Sang 	case ACPI_GSB_ACCESS_ATTRIB_BYTE:
62553f8f7c5SWolfram Sang 		if (action == ACPI_READ) {
62653f8f7c5SWolfram Sang 			status = i2c_smbus_read_byte_data(client, command);
62753f8f7c5SWolfram Sang 			if (status >= 0) {
62853f8f7c5SWolfram Sang 				gsb->bdata = status;
62953f8f7c5SWolfram Sang 				status = 0;
63053f8f7c5SWolfram Sang 			}
63153f8f7c5SWolfram Sang 		} else {
63253f8f7c5SWolfram Sang 			status = i2c_smbus_write_byte_data(client, command,
63353f8f7c5SWolfram Sang 					gsb->bdata);
63453f8f7c5SWolfram Sang 		}
63553f8f7c5SWolfram Sang 		break;
63653f8f7c5SWolfram Sang 
63753f8f7c5SWolfram Sang 	case ACPI_GSB_ACCESS_ATTRIB_WORD:
63853f8f7c5SWolfram Sang 		if (action == ACPI_READ) {
63953f8f7c5SWolfram Sang 			status = i2c_smbus_read_word_data(client, command);
64053f8f7c5SWolfram Sang 			if (status >= 0) {
64153f8f7c5SWolfram Sang 				gsb->wdata = status;
64253f8f7c5SWolfram Sang 				status = 0;
64353f8f7c5SWolfram Sang 			}
64453f8f7c5SWolfram Sang 		} else {
64553f8f7c5SWolfram Sang 			status = i2c_smbus_write_word_data(client, command,
64653f8f7c5SWolfram Sang 					gsb->wdata);
64753f8f7c5SWolfram Sang 		}
64853f8f7c5SWolfram Sang 		break;
64953f8f7c5SWolfram Sang 
65053f8f7c5SWolfram Sang 	case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
65153f8f7c5SWolfram Sang 		if (action == ACPI_READ) {
65253f8f7c5SWolfram Sang 			status = i2c_smbus_read_block_data(client, command,
65353f8f7c5SWolfram Sang 					gsb->data);
65453f8f7c5SWolfram Sang 			if (status >= 0) {
65553f8f7c5SWolfram Sang 				gsb->len = status;
65653f8f7c5SWolfram Sang 				status = 0;
65753f8f7c5SWolfram Sang 			}
65853f8f7c5SWolfram Sang 		} else {
65953f8f7c5SWolfram Sang 			status = i2c_smbus_write_block_data(client, command,
66053f8f7c5SWolfram Sang 					gsb->len, gsb->data);
66153f8f7c5SWolfram Sang 		}
66253f8f7c5SWolfram Sang 		break;
66353f8f7c5SWolfram Sang 
66453f8f7c5SWolfram Sang 	case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
66553f8f7c5SWolfram Sang 		if (action == ACPI_READ) {
66653f8f7c5SWolfram Sang 			status = acpi_gsb_i2c_read_bytes(client, command,
66753f8f7c5SWolfram Sang 					gsb->data, info->access_length);
66853f8f7c5SWolfram Sang 		} else {
66953f8f7c5SWolfram Sang 			status = acpi_gsb_i2c_write_bytes(client, command,
67053f8f7c5SWolfram Sang 					gsb->data, info->access_length);
67153f8f7c5SWolfram Sang 		}
67253f8f7c5SWolfram Sang 		break;
67353f8f7c5SWolfram Sang 
67453f8f7c5SWolfram Sang 	default:
67553f8f7c5SWolfram Sang 		dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
67653f8f7c5SWolfram Sang 			 accessor_type, client->addr);
67753f8f7c5SWolfram Sang 		ret = AE_BAD_PARAMETER;
67853f8f7c5SWolfram Sang 		goto err;
67953f8f7c5SWolfram Sang 	}
68053f8f7c5SWolfram Sang 
68153f8f7c5SWolfram Sang 	gsb->status = status;
68253f8f7c5SWolfram Sang 
68353f8f7c5SWolfram Sang  err:
68453f8f7c5SWolfram Sang 	kfree(client);
68553f8f7c5SWolfram Sang 	ACPI_FREE(ares);
68653f8f7c5SWolfram Sang 	return ret;
68753f8f7c5SWolfram Sang }
68853f8f7c5SWolfram Sang 
68953f8f7c5SWolfram Sang 
69053f8f7c5SWolfram Sang int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
69153f8f7c5SWolfram Sang {
69253f8f7c5SWolfram Sang 	acpi_handle handle;
69353f8f7c5SWolfram Sang 	struct i2c_acpi_handler_data *data;
69453f8f7c5SWolfram Sang 	acpi_status status;
69553f8f7c5SWolfram Sang 
69653f8f7c5SWolfram Sang 	if (!adapter->dev.parent)
69753f8f7c5SWolfram Sang 		return -ENODEV;
69853f8f7c5SWolfram Sang 
69953f8f7c5SWolfram Sang 	handle = ACPI_HANDLE(adapter->dev.parent);
70053f8f7c5SWolfram Sang 
70153f8f7c5SWolfram Sang 	if (!handle)
70253f8f7c5SWolfram Sang 		return -ENODEV;
70353f8f7c5SWolfram Sang 
70453f8f7c5SWolfram Sang 	data = kzalloc(sizeof(struct i2c_acpi_handler_data),
70553f8f7c5SWolfram Sang 			    GFP_KERNEL);
70653f8f7c5SWolfram Sang 	if (!data)
70753f8f7c5SWolfram Sang 		return -ENOMEM;
70853f8f7c5SWolfram Sang 
70953f8f7c5SWolfram Sang 	data->adapter = adapter;
71053f8f7c5SWolfram Sang 	status = acpi_bus_attach_private_data(handle, (void *)data);
71153f8f7c5SWolfram Sang 	if (ACPI_FAILURE(status)) {
71253f8f7c5SWolfram Sang 		kfree(data);
71353f8f7c5SWolfram Sang 		return -ENOMEM;
71453f8f7c5SWolfram Sang 	}
71553f8f7c5SWolfram Sang 
71653f8f7c5SWolfram Sang 	status = acpi_install_address_space_handler(handle,
71753f8f7c5SWolfram Sang 				ACPI_ADR_SPACE_GSBUS,
71853f8f7c5SWolfram Sang 				&i2c_acpi_space_handler,
71953f8f7c5SWolfram Sang 				NULL,
72053f8f7c5SWolfram Sang 				data);
72153f8f7c5SWolfram Sang 	if (ACPI_FAILURE(status)) {
72253f8f7c5SWolfram Sang 		dev_err(&adapter->dev, "Error installing i2c space handler\n");
72353f8f7c5SWolfram Sang 		acpi_bus_detach_private_data(handle);
72453f8f7c5SWolfram Sang 		kfree(data);
72553f8f7c5SWolfram Sang 		return -ENOMEM;
72653f8f7c5SWolfram Sang 	}
72753f8f7c5SWolfram Sang 
72853f8f7c5SWolfram Sang 	acpi_walk_dep_device_list(handle);
72953f8f7c5SWolfram Sang 	return 0;
73053f8f7c5SWolfram Sang }
73153f8f7c5SWolfram Sang 
73253f8f7c5SWolfram Sang void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
73353f8f7c5SWolfram Sang {
73453f8f7c5SWolfram Sang 	acpi_handle handle;
73553f8f7c5SWolfram Sang 	struct i2c_acpi_handler_data *data;
73653f8f7c5SWolfram Sang 	acpi_status status;
73753f8f7c5SWolfram Sang 
73853f8f7c5SWolfram Sang 	if (!adapter->dev.parent)
73953f8f7c5SWolfram Sang 		return;
74053f8f7c5SWolfram Sang 
74153f8f7c5SWolfram Sang 	handle = ACPI_HANDLE(adapter->dev.parent);
74253f8f7c5SWolfram Sang 
74353f8f7c5SWolfram Sang 	if (!handle)
74453f8f7c5SWolfram Sang 		return;
74553f8f7c5SWolfram Sang 
74653f8f7c5SWolfram Sang 	acpi_remove_address_space_handler(handle,
74753f8f7c5SWolfram Sang 				ACPI_ADR_SPACE_GSBUS,
74853f8f7c5SWolfram Sang 				&i2c_acpi_space_handler);
74953f8f7c5SWolfram Sang 
75053f8f7c5SWolfram Sang 	status = acpi_bus_get_private_data(handle, (void **)&data);
75153f8f7c5SWolfram Sang 	if (ACPI_SUCCESS(status))
75253f8f7c5SWolfram Sang 		kfree(data);
75353f8f7c5SWolfram Sang 
75453f8f7c5SWolfram Sang 	acpi_bus_detach_private_data(handle);
75553f8f7c5SWolfram Sang }
75653f8f7c5SWolfram Sang #endif /* CONFIG_ACPI_I2C_OPREGION */
757