Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux I2C core ACPI support code
0004  *
0005  * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
0006  */
0007 
0008 #include <linux/acpi.h>
0009 #include <linux/device.h>
0010 #include <linux/err.h>
0011 #include <linux/i2c.h>
0012 #include <linux/list.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 
0016 #include "i2c-core.h"
0017 
0018 struct i2c_acpi_handler_data {
0019     struct acpi_connection_info info;
0020     struct i2c_adapter *adapter;
0021 };
0022 
0023 struct gsb_buffer {
0024     u8  status;
0025     u8  len;
0026     union {
0027         u16 wdata;
0028         u8  bdata;
0029         u8  data[0];
0030     };
0031 } __packed;
0032 
0033 struct i2c_acpi_lookup {
0034     struct i2c_board_info *info;
0035     acpi_handle adapter_handle;
0036     acpi_handle device_handle;
0037     acpi_handle search_handle;
0038     int n;
0039     int index;
0040     u32 speed;
0041     u32 min_speed;
0042     u32 force_speed;
0043 };
0044 
0045 /**
0046  * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
0047  * @ares:   ACPI resource
0048  * @i2c:    Pointer to I2cSerialBus resource will be returned here
0049  *
0050  * Checks if the given ACPI resource is of type I2cSerialBus.
0051  * In this case, returns a pointer to it to the caller.
0052  *
0053  * Returns true if resource type is of I2cSerialBus, otherwise false.
0054  */
0055 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
0056                    struct acpi_resource_i2c_serialbus **i2c)
0057 {
0058     struct acpi_resource_i2c_serialbus *sb;
0059 
0060     if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
0061         return false;
0062 
0063     sb = &ares->data.i2c_serial_bus;
0064     if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
0065         return false;
0066 
0067     *i2c = sb;
0068     return true;
0069 }
0070 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
0071 
0072 static int i2c_acpi_resource_count(struct acpi_resource *ares, void *data)
0073 {
0074     struct acpi_resource_i2c_serialbus *sb;
0075     int *count = data;
0076 
0077     if (i2c_acpi_get_i2c_resource(ares, &sb))
0078         *count = *count + 1;
0079 
0080     return 1;
0081 }
0082 
0083 /**
0084  * i2c_acpi_client_count - Count the number of I2cSerialBus resources
0085  * @adev:   ACPI device
0086  *
0087  * Returns the number of I2cSerialBus resources in the ACPI-device's
0088  * resource-list; or a negative error code.
0089  */
0090 int i2c_acpi_client_count(struct acpi_device *adev)
0091 {
0092     int ret, count = 0;
0093     LIST_HEAD(r);
0094 
0095     ret = acpi_dev_get_resources(adev, &r, i2c_acpi_resource_count, &count);
0096     if (ret < 0)
0097         return ret;
0098 
0099     acpi_dev_free_resource_list(&r);
0100     return count;
0101 }
0102 EXPORT_SYMBOL_GPL(i2c_acpi_client_count);
0103 
0104 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
0105 {
0106     struct i2c_acpi_lookup *lookup = data;
0107     struct i2c_board_info *info = lookup->info;
0108     struct acpi_resource_i2c_serialbus *sb;
0109     acpi_status status;
0110 
0111     if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
0112         return 1;
0113 
0114     if (lookup->index != -1 && lookup->n++ != lookup->index)
0115         return 1;
0116 
0117     status = acpi_get_handle(lookup->device_handle,
0118                  sb->resource_source.string_ptr,
0119                  &lookup->adapter_handle);
0120     if (ACPI_FAILURE(status))
0121         return 1;
0122 
0123     info->addr = sb->slave_address;
0124     lookup->speed = sb->connection_speed;
0125     if (sb->access_mode == ACPI_I2C_10BIT_MODE)
0126         info->flags |= I2C_CLIENT_TEN;
0127 
0128     return 1;
0129 }
0130 
0131 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
0132     /*
0133      * ACPI video acpi_devices, which are handled by the acpi-video driver
0134      * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
0135      */
0136     { ACPI_VIDEO_HID, 0 },
0137     {}
0138 };
0139 
0140 static int i2c_acpi_do_lookup(struct acpi_device *adev,
0141                   struct i2c_acpi_lookup *lookup)
0142 {
0143     struct i2c_board_info *info = lookup->info;
0144     struct list_head resource_list;
0145     int ret;
0146 
0147     if (acpi_bus_get_status(adev))
0148         return -EINVAL;
0149 
0150     if (!acpi_dev_ready_for_enumeration(adev))
0151         return -ENODEV;
0152 
0153     if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
0154         return -ENODEV;
0155 
0156     memset(info, 0, sizeof(*info));
0157     lookup->device_handle = acpi_device_handle(adev);
0158 
0159     /* Look up for I2cSerialBus resource */
0160     INIT_LIST_HEAD(&resource_list);
0161     ret = acpi_dev_get_resources(adev, &resource_list,
0162                      i2c_acpi_fill_info, lookup);
0163     acpi_dev_free_resource_list(&resource_list);
0164 
0165     if (ret < 0 || !info->addr)
0166         return -EINVAL;
0167 
0168     return 0;
0169 }
0170 
0171 static int i2c_acpi_add_resource(struct acpi_resource *ares, void *data)
0172 {
0173     int *irq = data;
0174     struct resource r;
0175 
0176     if (*irq <= 0 && acpi_dev_resource_interrupt(ares, 0, &r))
0177         *irq = i2c_dev_irq_from_resources(&r, 1);
0178 
0179     return 1; /* No need to add resource to the list */
0180 }
0181 
0182 /**
0183  * i2c_acpi_get_irq - get device IRQ number from ACPI
0184  * @client: Pointer to the I2C client device
0185  *
0186  * Find the IRQ number used by a specific client device.
0187  *
0188  * Return: The IRQ number or an error code.
0189  */
0190 int i2c_acpi_get_irq(struct i2c_client *client)
0191 {
0192     struct acpi_device *adev = ACPI_COMPANION(&client->dev);
0193     struct list_head resource_list;
0194     int irq = -ENOENT;
0195     int ret;
0196 
0197     INIT_LIST_HEAD(&resource_list);
0198 
0199     ret = acpi_dev_get_resources(adev, &resource_list,
0200                      i2c_acpi_add_resource, &irq);
0201     if (ret < 0)
0202         return ret;
0203 
0204     acpi_dev_free_resource_list(&resource_list);
0205 
0206     if (irq == -ENOENT)
0207         irq = acpi_dev_gpio_irq_get(adev, 0);
0208 
0209     return irq;
0210 }
0211 
0212 static int i2c_acpi_get_info(struct acpi_device *adev,
0213                  struct i2c_board_info *info,
0214                  struct i2c_adapter *adapter,
0215                  acpi_handle *adapter_handle)
0216 {
0217     struct i2c_acpi_lookup lookup;
0218     int ret;
0219 
0220     memset(&lookup, 0, sizeof(lookup));
0221     lookup.info = info;
0222     lookup.index = -1;
0223 
0224     if (acpi_device_enumerated(adev))
0225         return -EINVAL;
0226 
0227     ret = i2c_acpi_do_lookup(adev, &lookup);
0228     if (ret)
0229         return ret;
0230 
0231     if (adapter) {
0232         /* The adapter must match the one in I2cSerialBus() connector */
0233         if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
0234             return -ENODEV;
0235     } else {
0236         struct acpi_device *adapter_adev;
0237 
0238         /* The adapter must be present */
0239         adapter_adev = acpi_fetch_acpi_dev(lookup.adapter_handle);
0240         if (!adapter_adev)
0241             return -ENODEV;
0242         if (acpi_bus_get_status(adapter_adev) ||
0243             !adapter_adev->status.present)
0244             return -ENODEV;
0245     }
0246 
0247     info->fwnode = acpi_fwnode_handle(adev);
0248     if (adapter_handle)
0249         *adapter_handle = lookup.adapter_handle;
0250 
0251     acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
0252               sizeof(info->type));
0253 
0254     return 0;
0255 }
0256 
0257 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
0258                      struct acpi_device *adev,
0259                      struct i2c_board_info *info)
0260 {
0261     /*
0262      * Skip registration on boards where the ACPI tables are
0263      * known to contain bogus I2C devices.
0264      */
0265     if (acpi_quirk_skip_i2c_client_enumeration(adev))
0266         return;
0267 
0268     adev->power.flags.ignore_parent = true;
0269     acpi_device_set_enumerated(adev);
0270 
0271     if (IS_ERR(i2c_new_client_device(adapter, info)))
0272         adev->power.flags.ignore_parent = false;
0273 }
0274 
0275 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
0276                        void *data, void **return_value)
0277 {
0278     struct i2c_adapter *adapter = data;
0279     struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
0280     struct i2c_board_info info;
0281 
0282     if (!adev || i2c_acpi_get_info(adev, &info, adapter, NULL))
0283         return AE_OK;
0284 
0285     i2c_acpi_register_device(adapter, adev, &info);
0286 
0287     return AE_OK;
0288 }
0289 
0290 #define I2C_ACPI_MAX_SCAN_DEPTH 32
0291 
0292 /**
0293  * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
0294  * @adap: pointer to adapter
0295  *
0296  * Enumerate all I2C slave devices behind this adapter by walking the ACPI
0297  * namespace. When a device is found it will be added to the Linux device
0298  * model and bound to the corresponding ACPI handle.
0299  */
0300 void i2c_acpi_register_devices(struct i2c_adapter *adap)
0301 {
0302     struct acpi_device *adev;
0303     acpi_status status;
0304 
0305     if (!has_acpi_companion(&adap->dev))
0306         return;
0307 
0308     status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
0309                      I2C_ACPI_MAX_SCAN_DEPTH,
0310                      i2c_acpi_add_device, NULL,
0311                      adap, NULL);
0312     if (ACPI_FAILURE(status))
0313         dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
0314 
0315     if (!adap->dev.parent)
0316         return;
0317 
0318     adev = ACPI_COMPANION(adap->dev.parent);
0319     if (!adev)
0320         return;
0321 
0322     acpi_dev_clear_dependencies(adev);
0323 }
0324 
0325 static const struct acpi_device_id i2c_acpi_force_400khz_device_ids[] = {
0326     /*
0327      * These Silead touchscreen controllers only work at 400KHz, for
0328      * some reason they do not work at 100KHz. On some devices the ACPI
0329      * tables list another device at their bus as only being capable
0330      * of 100KHz, testing has shown that these other devices work fine
0331      * at 400KHz (as can be expected of any recent i2c hw) so we force
0332      * the speed of the bus to 400 KHz if a Silead device is present.
0333      */
0334     { "MSSL1680", 0 },
0335     {}
0336 };
0337 
0338 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
0339                        void *data, void **return_value)
0340 {
0341     struct i2c_acpi_lookup *lookup = data;
0342     struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
0343 
0344     if (!adev || i2c_acpi_do_lookup(adev, lookup))
0345         return AE_OK;
0346 
0347     if (lookup->search_handle != lookup->adapter_handle)
0348         return AE_OK;
0349 
0350     if (lookup->speed <= lookup->min_speed)
0351         lookup->min_speed = lookup->speed;
0352 
0353     if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
0354         lookup->force_speed = I2C_MAX_FAST_MODE_FREQ;
0355 
0356     return AE_OK;
0357 }
0358 
0359 /**
0360  * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
0361  * @dev: The device owning the bus
0362  *
0363  * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
0364  * devices connected to this bus and use the speed of slowest device.
0365  *
0366  * Returns the speed in Hz or zero
0367  */
0368 u32 i2c_acpi_find_bus_speed(struct device *dev)
0369 {
0370     struct i2c_acpi_lookup lookup;
0371     struct i2c_board_info dummy;
0372     acpi_status status;
0373 
0374     if (!has_acpi_companion(dev))
0375         return 0;
0376 
0377     memset(&lookup, 0, sizeof(lookup));
0378     lookup.search_handle = ACPI_HANDLE(dev);
0379     lookup.min_speed = UINT_MAX;
0380     lookup.info = &dummy;
0381     lookup.index = -1;
0382 
0383     status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
0384                      I2C_ACPI_MAX_SCAN_DEPTH,
0385                      i2c_acpi_lookup_speed, NULL,
0386                      &lookup, NULL);
0387 
0388     if (ACPI_FAILURE(status)) {
0389         dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
0390         return 0;
0391     }
0392 
0393     if (lookup.force_speed) {
0394         if (lookup.force_speed != lookup.min_speed)
0395             dev_warn(dev, FW_BUG "DSDT uses known not-working I2C bus speed %d, forcing it to %d\n",
0396                  lookup.min_speed, lookup.force_speed);
0397         return lookup.force_speed;
0398     } else if (lookup.min_speed != UINT_MAX) {
0399         return lookup.min_speed;
0400     } else {
0401         return 0;
0402     }
0403 }
0404 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
0405 
0406 struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
0407 {
0408     struct i2c_adapter *adapter;
0409     struct device *dev;
0410 
0411     dev = bus_find_device(&i2c_bus_type, NULL, handle, device_match_acpi_handle);
0412     if (!dev)
0413         return NULL;
0414 
0415     adapter = i2c_verify_adapter(dev);
0416     if (!adapter)
0417         put_device(dev);
0418 
0419     return adapter;
0420 }
0421 EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
0422 
0423 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
0424 {
0425     struct device *dev;
0426     struct i2c_client *client;
0427 
0428     dev = bus_find_device_by_acpi_dev(&i2c_bus_type, adev);
0429     if (!dev)
0430         return NULL;
0431 
0432     client = i2c_verify_client(dev);
0433     if (!client)
0434         put_device(dev);
0435 
0436     return client;
0437 }
0438 
0439 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
0440                void *arg)
0441 {
0442     struct acpi_device *adev = arg;
0443     struct i2c_board_info info;
0444     acpi_handle adapter_handle;
0445     struct i2c_adapter *adapter;
0446     struct i2c_client *client;
0447 
0448     switch (value) {
0449     case ACPI_RECONFIG_DEVICE_ADD:
0450         if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
0451             break;
0452 
0453         adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
0454         if (!adapter)
0455             break;
0456 
0457         i2c_acpi_register_device(adapter, adev, &info);
0458         put_device(&adapter->dev);
0459         break;
0460     case ACPI_RECONFIG_DEVICE_REMOVE:
0461         if (!acpi_device_enumerated(adev))
0462             break;
0463 
0464         client = i2c_acpi_find_client_by_adev(adev);
0465         if (!client)
0466             break;
0467 
0468         i2c_unregister_device(client);
0469         put_device(&client->dev);
0470         break;
0471     }
0472 
0473     return NOTIFY_OK;
0474 }
0475 
0476 struct notifier_block i2c_acpi_notifier = {
0477     .notifier_call = i2c_acpi_notify,
0478 };
0479 
0480 /**
0481  * i2c_acpi_new_device_by_fwnode - Create i2c-client for the Nth I2cSerialBus resource
0482  * @fwnode:  fwnode with the ACPI resources to get the client from
0483  * @index:   Index of ACPI resource to get
0484  * @info:    describes the I2C device; note this is modified (addr gets set)
0485  * Context: can sleep
0486  *
0487  * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
0488  * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
0489  * resources, in that case this function can be used to create an i2c-client
0490  * for other I2cSerialBus resources in the Current Resource Settings table.
0491  *
0492  * Also see i2c_new_client_device, which this function calls to create the
0493  * i2c-client.
0494  *
0495  * Returns a pointer to the new i2c-client, or error pointer in case of failure.
0496  * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
0497  */
0498 struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode,
0499                          int index,
0500                          struct i2c_board_info *info)
0501 {
0502     struct i2c_acpi_lookup lookup;
0503     struct i2c_adapter *adapter;
0504     struct acpi_device *adev;
0505     LIST_HEAD(resource_list);
0506     int ret;
0507 
0508     adev = to_acpi_device_node(fwnode);
0509     if (!adev)
0510         return ERR_PTR(-ENODEV);
0511 
0512     memset(&lookup, 0, sizeof(lookup));
0513     lookup.info = info;
0514     lookup.device_handle = acpi_device_handle(adev);
0515     lookup.index = index;
0516 
0517     ret = acpi_dev_get_resources(adev, &resource_list,
0518                      i2c_acpi_fill_info, &lookup);
0519     if (ret < 0)
0520         return ERR_PTR(ret);
0521 
0522     acpi_dev_free_resource_list(&resource_list);
0523 
0524     if (!info->addr)
0525         return ERR_PTR(-EADDRNOTAVAIL);
0526 
0527     adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
0528     if (!adapter)
0529         return ERR_PTR(-EPROBE_DEFER);
0530 
0531     return i2c_new_client_device(adapter, info);
0532 }
0533 EXPORT_SYMBOL_GPL(i2c_acpi_new_device_by_fwnode);
0534 
0535 bool i2c_acpi_waive_d0_probe(struct device *dev)
0536 {
0537     struct i2c_driver *driver = to_i2c_driver(dev->driver);
0538     struct acpi_device *adev = ACPI_COMPANION(dev);
0539 
0540     return driver->flags & I2C_DRV_ACPI_WAIVE_D0_PROBE &&
0541         adev && adev->power.state_for_enumeration >= adev->power.state;
0542 }
0543 EXPORT_SYMBOL_GPL(i2c_acpi_waive_d0_probe);
0544 
0545 #ifdef CONFIG_ACPI_I2C_OPREGION
0546 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
0547         u8 cmd, u8 *data, u8 data_len)
0548 {
0549 
0550     struct i2c_msg msgs[2];
0551     int ret;
0552     u8 *buffer;
0553 
0554     buffer = kzalloc(data_len, GFP_KERNEL);
0555     if (!buffer)
0556         return AE_NO_MEMORY;
0557 
0558     msgs[0].addr = client->addr;
0559     msgs[0].flags = client->flags;
0560     msgs[0].len = 1;
0561     msgs[0].buf = &cmd;
0562 
0563     msgs[1].addr = client->addr;
0564     msgs[1].flags = client->flags | I2C_M_RD;
0565     msgs[1].len = data_len;
0566     msgs[1].buf = buffer;
0567 
0568     ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0569     if (ret < 0) {
0570         /* Getting a NACK is unfortunately normal with some DSTDs */
0571         if (ret == -EREMOTEIO)
0572             dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
0573                 data_len, client->addr, cmd, ret);
0574         else
0575             dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
0576                 data_len, client->addr, cmd, ret);
0577     /* 2 transfers must have completed successfully */
0578     } else if (ret == 2) {
0579         memcpy(data, buffer, data_len);
0580         ret = 0;
0581     } else {
0582         ret = -EIO;
0583     }
0584 
0585     kfree(buffer);
0586     return ret;
0587 }
0588 
0589 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
0590         u8 cmd, u8 *data, u8 data_len)
0591 {
0592 
0593     struct i2c_msg msgs[1];
0594     u8 *buffer;
0595     int ret = AE_OK;
0596 
0597     buffer = kzalloc(data_len + 1, GFP_KERNEL);
0598     if (!buffer)
0599         return AE_NO_MEMORY;
0600 
0601     buffer[0] = cmd;
0602     memcpy(buffer + 1, data, data_len);
0603 
0604     msgs[0].addr = client->addr;
0605     msgs[0].flags = client->flags;
0606     msgs[0].len = data_len + 1;
0607     msgs[0].buf = buffer;
0608 
0609     ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0610 
0611     kfree(buffer);
0612 
0613     if (ret < 0) {
0614         dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
0615         return ret;
0616     }
0617 
0618     /* 1 transfer must have completed successfully */
0619     return (ret == 1) ? 0 : -EIO;
0620 }
0621 
0622 static acpi_status
0623 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
0624             u32 bits, u64 *value64,
0625             void *handler_context, void *region_context)
0626 {
0627     struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
0628     struct i2c_acpi_handler_data *data = handler_context;
0629     struct acpi_connection_info *info = &data->info;
0630     struct acpi_resource_i2c_serialbus *sb;
0631     struct i2c_adapter *adapter = data->adapter;
0632     struct i2c_client *client;
0633     struct acpi_resource *ares;
0634     u32 accessor_type = function >> 16;
0635     u8 action = function & ACPI_IO_MASK;
0636     acpi_status ret;
0637     int status;
0638 
0639     ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
0640     if (ACPI_FAILURE(ret))
0641         return ret;
0642 
0643     client = kzalloc(sizeof(*client), GFP_KERNEL);
0644     if (!client) {
0645         ret = AE_NO_MEMORY;
0646         goto err;
0647     }
0648 
0649     if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
0650         ret = AE_BAD_PARAMETER;
0651         goto err;
0652     }
0653 
0654     client->adapter = adapter;
0655     client->addr = sb->slave_address;
0656 
0657     if (sb->access_mode == ACPI_I2C_10BIT_MODE)
0658         client->flags |= I2C_CLIENT_TEN;
0659 
0660     switch (accessor_type) {
0661     case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
0662         if (action == ACPI_READ) {
0663             status = i2c_smbus_read_byte(client);
0664             if (status >= 0) {
0665                 gsb->bdata = status;
0666                 status = 0;
0667             }
0668         } else {
0669             status = i2c_smbus_write_byte(client, gsb->bdata);
0670         }
0671         break;
0672 
0673     case ACPI_GSB_ACCESS_ATTRIB_BYTE:
0674         if (action == ACPI_READ) {
0675             status = i2c_smbus_read_byte_data(client, command);
0676             if (status >= 0) {
0677                 gsb->bdata = status;
0678                 status = 0;
0679             }
0680         } else {
0681             status = i2c_smbus_write_byte_data(client, command,
0682                     gsb->bdata);
0683         }
0684         break;
0685 
0686     case ACPI_GSB_ACCESS_ATTRIB_WORD:
0687         if (action == ACPI_READ) {
0688             status = i2c_smbus_read_word_data(client, command);
0689             if (status >= 0) {
0690                 gsb->wdata = status;
0691                 status = 0;
0692             }
0693         } else {
0694             status = i2c_smbus_write_word_data(client, command,
0695                     gsb->wdata);
0696         }
0697         break;
0698 
0699     case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
0700         if (action == ACPI_READ) {
0701             status = i2c_smbus_read_block_data(client, command,
0702                     gsb->data);
0703             if (status >= 0) {
0704                 gsb->len = status;
0705                 status = 0;
0706             }
0707         } else {
0708             status = i2c_smbus_write_block_data(client, command,
0709                     gsb->len, gsb->data);
0710         }
0711         break;
0712 
0713     case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
0714         if (action == ACPI_READ) {
0715             status = acpi_gsb_i2c_read_bytes(client, command,
0716                     gsb->data, info->access_length);
0717         } else {
0718             status = acpi_gsb_i2c_write_bytes(client, command,
0719                     gsb->data, info->access_length);
0720         }
0721         break;
0722 
0723     default:
0724         dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
0725              accessor_type, client->addr);
0726         ret = AE_BAD_PARAMETER;
0727         goto err;
0728     }
0729 
0730     gsb->status = status;
0731 
0732  err:
0733     kfree(client);
0734     ACPI_FREE(ares);
0735     return ret;
0736 }
0737 
0738 
0739 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
0740 {
0741     acpi_handle handle;
0742     struct i2c_acpi_handler_data *data;
0743     acpi_status status;
0744 
0745     if (!adapter->dev.parent)
0746         return -ENODEV;
0747 
0748     handle = ACPI_HANDLE(adapter->dev.parent);
0749 
0750     if (!handle)
0751         return -ENODEV;
0752 
0753     data = kzalloc(sizeof(struct i2c_acpi_handler_data),
0754                 GFP_KERNEL);
0755     if (!data)
0756         return -ENOMEM;
0757 
0758     data->adapter = adapter;
0759     status = acpi_bus_attach_private_data(handle, (void *)data);
0760     if (ACPI_FAILURE(status)) {
0761         kfree(data);
0762         return -ENOMEM;
0763     }
0764 
0765     status = acpi_install_address_space_handler(handle,
0766                 ACPI_ADR_SPACE_GSBUS,
0767                 &i2c_acpi_space_handler,
0768                 NULL,
0769                 data);
0770     if (ACPI_FAILURE(status)) {
0771         dev_err(&adapter->dev, "Error installing i2c space handler\n");
0772         acpi_bus_detach_private_data(handle);
0773         kfree(data);
0774         return -ENOMEM;
0775     }
0776 
0777     return 0;
0778 }
0779 
0780 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
0781 {
0782     acpi_handle handle;
0783     struct i2c_acpi_handler_data *data;
0784     acpi_status status;
0785 
0786     if (!adapter->dev.parent)
0787         return;
0788 
0789     handle = ACPI_HANDLE(adapter->dev.parent);
0790 
0791     if (!handle)
0792         return;
0793 
0794     acpi_remove_address_space_handler(handle,
0795                 ACPI_ADR_SPACE_GSBUS,
0796                 &i2c_acpi_space_handler);
0797 
0798     status = acpi_bus_get_private_data(handle, (void **)&data);
0799     if (ACPI_SUCCESS(status))
0800         kfree(data);
0801 
0802     acpi_bus_detach_private_data(handle);
0803 }
0804 #endif /* CONFIG_ACPI_I2C_OPREGION */