4 * Copyright (C) 2014 Intel Corp
6 * Author: Lan Tianyu <tianyu.lan@intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #define pr_fmt(fmt) "I2C/ACPI : " fmt
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/acpi.h>
25 struct acpi_i2c_handler_data {
26 struct acpi_connection_info info;
27 struct i2c_adapter *adapter;
40 static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
42 struct i2c_board_info *info = data;
44 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
45 struct acpi_resource_i2c_serialbus *sb;
47 sb = &ares->data.i2c_serial_bus;
48 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
49 info->addr = sb->slave_address;
50 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
51 info->flags |= I2C_CLIENT_TEN;
53 } else if (info->irq < 0) {
56 if (acpi_dev_resource_interrupt(ares, 0, &r))
60 /* Tell the ACPI core to skip this resource */
64 static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
65 void *data, void **return_value)
67 struct i2c_adapter *adapter = data;
68 struct list_head resource_list;
69 struct i2c_board_info info;
70 struct acpi_device *adev;
73 if (acpi_bus_get_device(handle, &adev))
75 if (acpi_bus_get_status(adev) || !adev->status.present)
78 memset(&info, 0, sizeof(info));
79 info.acpi_node.companion = adev;
82 INIT_LIST_HEAD(&resource_list);
83 ret = acpi_dev_get_resources(adev, &resource_list,
84 acpi_i2c_add_resource, &info);
85 acpi_dev_free_resource_list(&resource_list);
87 if (ret < 0 || !info.addr)
90 adev->power.flags.ignore_parent = true;
91 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
92 if (!i2c_new_device(adapter, &info)) {
93 adev->power.flags.ignore_parent = false;
94 dev_err(&adapter->dev,
95 "failed to add I2C device %s from ACPI\n",
96 dev_name(&adev->dev));
103 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
104 * @adap: pointer to adapter
106 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
107 * namespace. When a device is found it will be added to the Linux device
108 * model and bound to the corresponding ACPI handle.
110 void acpi_i2c_register_devices(struct i2c_adapter *adap)
115 if (!adap->dev.parent)
118 handle = ACPI_HANDLE(adap->dev.parent);
122 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
123 acpi_i2c_add_device, NULL,
125 if (ACPI_FAILURE(status))
126 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
129 #ifdef CONFIG_ACPI_I2C_OPREGION
130 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
131 u8 cmd, u8 *data, u8 data_len)
134 struct i2c_msg msgs[2];
138 buffer = kzalloc(data_len, GFP_KERNEL);
142 msgs[0].addr = client->addr;
143 msgs[0].flags = client->flags;
147 msgs[1].addr = client->addr;
148 msgs[1].flags = client->flags | I2C_M_RD;
149 msgs[1].len = data_len;
150 msgs[1].buf = buffer;
152 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
154 dev_err(&client->adapter->dev, "i2c read failed\n");
156 memcpy(data, buffer, data_len);
162 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
163 u8 cmd, u8 *data, u8 data_len)
166 struct i2c_msg msgs[1];
170 buffer = kzalloc(data_len + 1, GFP_KERNEL);
175 memcpy(buffer + 1, data, data_len);
177 msgs[0].addr = client->addr;
178 msgs[0].flags = client->flags;
179 msgs[0].len = data_len + 1;
180 msgs[0].buf = buffer;
182 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
184 dev_err(&client->adapter->dev, "i2c write failed\n");
191 acpi_i2c_space_handler(u32 function, acpi_physical_address command,
192 u32 bits, u64 *value64,
193 void *handler_context, void *region_context)
195 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
196 struct acpi_i2c_handler_data *data = handler_context;
197 struct acpi_connection_info *info = &data->info;
198 struct acpi_resource_i2c_serialbus *sb;
199 struct i2c_adapter *adapter = data->adapter;
200 struct i2c_client client;
201 struct acpi_resource *ares;
202 u32 accessor_type = function >> 16;
203 u8 action = function & ACPI_IO_MASK;
204 acpi_status ret = AE_OK;
207 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
208 if (ACPI_FAILURE(ret))
211 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
212 ret = AE_BAD_PARAMETER;
216 sb = &ares->data.i2c_serial_bus;
217 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
218 ret = AE_BAD_PARAMETER;
222 memset(&client, 0, sizeof(client));
223 client.adapter = adapter;
224 client.addr = sb->slave_address;
227 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
228 client.flags |= I2C_CLIENT_TEN;
230 switch (accessor_type) {
231 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
232 if (action == ACPI_READ) {
233 status = i2c_smbus_read_byte(&client);
239 status = i2c_smbus_write_byte(&client, gsb->bdata);
243 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
244 if (action == ACPI_READ) {
245 status = i2c_smbus_read_byte_data(&client, command);
251 status = i2c_smbus_write_byte_data(&client, command,
256 case ACPI_GSB_ACCESS_ATTRIB_WORD:
257 if (action == ACPI_READ) {
258 status = i2c_smbus_read_word_data(&client, command);
264 status = i2c_smbus_write_word_data(&client, command,
269 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
270 if (action == ACPI_READ) {
271 status = i2c_smbus_read_block_data(&client, command,
278 status = i2c_smbus_write_block_data(&client, command,
279 gsb->len, gsb->data);
283 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
284 if (action == ACPI_READ) {
285 status = acpi_gsb_i2c_read_bytes(&client, command,
286 gsb->data, info->access_length);
290 status = acpi_gsb_i2c_write_bytes(&client, command,
291 gsb->data, info->access_length);
296 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
297 ret = AE_BAD_PARAMETER;
301 gsb->status = status;
309 int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
311 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
312 struct acpi_i2c_handler_data *data;
318 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
323 data->adapter = adapter;
324 status = acpi_bus_attach_private_data(handle, (void *)data);
325 if (ACPI_FAILURE(status)) {
330 status = acpi_install_address_space_handler(handle,
331 ACPI_ADR_SPACE_GSBUS,
332 &acpi_i2c_space_handler,
335 if (ACPI_FAILURE(status)) {
336 dev_err(&adapter->dev, "Error installing i2c space handler\n");
337 acpi_bus_detach_private_data(handle);
345 void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
347 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
348 struct acpi_i2c_handler_data *data;
354 acpi_remove_address_space_handler(handle,
355 ACPI_ADR_SPACE_GSBUS,
356 &acpi_i2c_space_handler);
358 status = acpi_bus_get_private_data(handle, (void **)&data);
359 if (ACPI_SUCCESS(status))
362 acpi_bus_detach_private_data(handle);