Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Linux I2C core ACPI support code |
| 3 | * |
| 4 | * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com> |
| 5 | * |
| 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) |
| 9 | * any later version. |
| 10 | */ |
| 11 | |
| 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> |
| 19 | |
| 20 | #include "i2c-core.h" |
| 21 | |
| 22 | struct i2c_acpi_handler_data { |
| 23 | struct acpi_connection_info info; |
| 24 | struct i2c_adapter *adapter; |
| 25 | }; |
| 26 | |
| 27 | struct gsb_buffer { |
| 28 | u8 status; |
| 29 | u8 len; |
| 30 | union { |
| 31 | u16 wdata; |
| 32 | u8 bdata; |
| 33 | u8 data[0]; |
| 34 | }; |
| 35 | } __packed; |
| 36 | |
| 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; |
| 42 | int n; |
| 43 | int index; |
| 44 | u32 speed; |
| 45 | u32 min_speed; |
| 46 | }; |
| 47 | |
Andy Shevchenko | 0d5102f | 2018-11-28 13:45:29 +0200 | [diff] [blame] | 48 | /** |
| 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 |
| 52 | * |
| 53 | * Checks if the given ACPI resource is of type I2cSerialBus. |
| 54 | * In this case, returns a pointer to it to the caller. |
| 55 | * |
| 56 | * Returns true if resource type is of I2cSerialBus, otherwise false. |
| 57 | */ |
| 58 | bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares, |
| 59 | struct acpi_resource_i2c_serialbus **i2c) |
| 60 | { |
| 61 | struct acpi_resource_i2c_serialbus *sb; |
| 62 | |
| 63 | if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) |
| 64 | return false; |
| 65 | |
| 66 | sb = &ares->data.i2c_serial_bus; |
| 67 | if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) |
| 68 | return false; |
| 69 | |
| 70 | *i2c = sb; |
| 71 | return true; |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource); |
| 74 | |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 75 | static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data) |
| 76 | { |
| 77 | struct i2c_acpi_lookup *lookup = data; |
| 78 | struct i2c_board_info *info = lookup->info; |
| 79 | struct acpi_resource_i2c_serialbus *sb; |
| 80 | acpi_status status; |
| 81 | |
Andy Shevchenko | 0d5102f | 2018-11-28 13:45:29 +0200 | [diff] [blame] | 82 | if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb)) |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 83 | return 1; |
| 84 | |
| 85 | if (lookup->index != -1 && lookup->n++ != lookup->index) |
| 86 | return 1; |
| 87 | |
| 88 | status = acpi_get_handle(lookup->device_handle, |
| 89 | sb->resource_source.string_ptr, |
| 90 | &lookup->adapter_handle); |
Andy Shevchenko | 5f59d6a | 2018-11-28 13:45:28 +0200 | [diff] [blame] | 91 | if (ACPI_FAILURE(status)) |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 92 | return 1; |
| 93 | |
| 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; |
| 98 | |
| 99 | return 1; |
| 100 | } |
| 101 | |
Hans de Goede | 3a4991a | 2017-07-04 15:04:48 +0200 | [diff] [blame] | 102 | static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = { |
| 103 | /* |
| 104 | * ACPI video acpi_devices, which are handled by the acpi-video driver |
| 105 | * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these. |
| 106 | */ |
| 107 | { ACPI_VIDEO_HID, 0 }, |
| 108 | {} |
| 109 | }; |
| 110 | |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 111 | static int i2c_acpi_do_lookup(struct acpi_device *adev, |
| 112 | struct i2c_acpi_lookup *lookup) |
| 113 | { |
| 114 | struct i2c_board_info *info = lookup->info; |
| 115 | struct list_head resource_list; |
| 116 | int ret; |
| 117 | |
| 118 | if (acpi_bus_get_status(adev) || !adev->status.present || |
| 119 | acpi_device_enumerated(adev)) |
| 120 | return -EINVAL; |
| 121 | |
Hans de Goede | 3a4991a | 2017-07-04 15:04:48 +0200 | [diff] [blame] | 122 | if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0) |
| 123 | return -ENODEV; |
| 124 | |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 125 | memset(info, 0, sizeof(*info)); |
| 126 | lookup->device_handle = acpi_device_handle(adev); |
| 127 | |
| 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); |
| 133 | |
| 134 | if (ret < 0 || !info->addr) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 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) |
| 144 | { |
| 145 | struct list_head resource_list; |
| 146 | struct resource_entry *entry; |
| 147 | struct i2c_acpi_lookup lookup; |
| 148 | int ret; |
| 149 | |
| 150 | memset(&lookup, 0, sizeof(lookup)); |
| 151 | lookup.info = info; |
| 152 | lookup.index = -1; |
| 153 | |
| 154 | ret = i2c_acpi_do_lookup(adev, &lookup); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
| 158 | if (adapter) { |
| 159 | /* The adapter must match the one in I2cSerialBus() connector */ |
| 160 | if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle) |
| 161 | return -ENODEV; |
| 162 | } else { |
| 163 | struct acpi_device *adapter_adev; |
| 164 | |
| 165 | /* The adapter must be present */ |
| 166 | if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev)) |
| 167 | return -ENODEV; |
| 168 | if (acpi_bus_get_status(adapter_adev) || |
| 169 | !adapter_adev->status.present) |
| 170 | return -ENODEV; |
| 171 | } |
| 172 | |
| 173 | info->fwnode = acpi_fwnode_handle(adev); |
| 174 | if (adapter_handle) |
| 175 | *adapter_handle = lookup.adapter_handle; |
| 176 | |
| 177 | /* Then fill IRQ number if any */ |
| 178 | INIT_LIST_HEAD(&resource_list); |
| 179 | ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); |
| 180 | if (ret < 0) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | resource_list_for_each_entry(entry, &resource_list) { |
| 184 | if (resource_type(entry->res) == IORESOURCE_IRQ) { |
| 185 | info->irq = entry->res->start; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | acpi_dev_free_resource_list(&resource_list); |
| 191 | |
| 192 | acpi_set_modalias(adev, dev_name(&adev->dev), info->type, |
| 193 | sizeof(info->type)); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static void i2c_acpi_register_device(struct i2c_adapter *adapter, |
| 199 | struct acpi_device *adev, |
| 200 | struct i2c_board_info *info) |
| 201 | { |
| 202 | adev->power.flags.ignore_parent = true; |
| 203 | acpi_device_set_enumerated(adev); |
| 204 | |
| 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)); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level, |
| 214 | void *data, void **return_value) |
| 215 | { |
| 216 | struct i2c_adapter *adapter = data; |
| 217 | struct acpi_device *adev; |
| 218 | struct i2c_board_info info; |
| 219 | |
| 220 | if (acpi_bus_get_device(handle, &adev)) |
| 221 | return AE_OK; |
| 222 | |
| 223 | if (i2c_acpi_get_info(adev, &info, adapter, NULL)) |
| 224 | return AE_OK; |
| 225 | |
| 226 | i2c_acpi_register_device(adapter, adev, &info); |
| 227 | |
| 228 | return AE_OK; |
| 229 | } |
| 230 | |
| 231 | #define I2C_ACPI_MAX_SCAN_DEPTH 32 |
| 232 | |
| 233 | /** |
| 234 | * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter |
| 235 | * @adap: pointer to adapter |
| 236 | * |
| 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. |
| 240 | */ |
| 241 | void i2c_acpi_register_devices(struct i2c_adapter *adap) |
| 242 | { |
| 243 | acpi_status status; |
| 244 | |
| 245 | if (!has_acpi_companion(&adap->dev)) |
| 246 | return; |
| 247 | |
| 248 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 249 | I2C_ACPI_MAX_SCAN_DEPTH, |
| 250 | i2c_acpi_add_device, NULL, |
| 251 | adap, NULL); |
| 252 | if (ACPI_FAILURE(status)) |
| 253 | dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); |
| 254 | } |
| 255 | |
Andy Shevchenko | c64ffff | 2017-07-17 17:13:28 +0300 | [diff] [blame] | 256 | const struct acpi_device_id * |
| 257 | i2c_acpi_match_device(const struct acpi_device_id *matches, |
| 258 | struct i2c_client *client) |
| 259 | { |
| 260 | if (!(client && matches)) |
| 261 | return NULL; |
| 262 | |
| 263 | return acpi_match_device(matches, &client->dev); |
| 264 | } |
| 265 | |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 266 | static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level, |
| 267 | void *data, void **return_value) |
| 268 | { |
| 269 | struct i2c_acpi_lookup *lookup = data; |
| 270 | struct acpi_device *adev; |
| 271 | |
| 272 | if (acpi_bus_get_device(handle, &adev)) |
| 273 | return AE_OK; |
| 274 | |
| 275 | if (i2c_acpi_do_lookup(adev, lookup)) |
| 276 | return AE_OK; |
| 277 | |
| 278 | if (lookup->search_handle != lookup->adapter_handle) |
| 279 | return AE_OK; |
| 280 | |
| 281 | if (lookup->speed <= lookup->min_speed) |
| 282 | lookup->min_speed = lookup->speed; |
| 283 | |
| 284 | return AE_OK; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI |
| 289 | * @dev: The device owning the bus |
| 290 | * |
| 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. |
| 293 | * |
| 294 | * Returns the speed in Hz or zero |
| 295 | */ |
| 296 | u32 i2c_acpi_find_bus_speed(struct device *dev) |
| 297 | { |
| 298 | struct i2c_acpi_lookup lookup; |
| 299 | struct i2c_board_info dummy; |
| 300 | acpi_status status; |
| 301 | |
| 302 | if (!has_acpi_companion(dev)) |
| 303 | return 0; |
| 304 | |
| 305 | memset(&lookup, 0, sizeof(lookup)); |
| 306 | lookup.search_handle = ACPI_HANDLE(dev); |
| 307 | lookup.min_speed = UINT_MAX; |
| 308 | lookup.info = &dummy; |
| 309 | lookup.index = -1; |
| 310 | |
| 311 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 312 | I2C_ACPI_MAX_SCAN_DEPTH, |
| 313 | i2c_acpi_lookup_speed, NULL, |
| 314 | &lookup, NULL); |
| 315 | |
| 316 | if (ACPI_FAILURE(status)) { |
| 317 | dev_warn(dev, "unable to find I2C bus speed from ACPI\n"); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0; |
| 322 | } |
| 323 | EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed); |
| 324 | |
Andy Shevchenko | c64ffff | 2017-07-17 17:13:28 +0300 | [diff] [blame] | 325 | static int i2c_acpi_find_match_adapter(struct device *dev, void *data) |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 326 | { |
| 327 | struct i2c_adapter *adapter = i2c_verify_adapter(dev); |
| 328 | |
| 329 | if (!adapter) |
| 330 | return 0; |
| 331 | |
| 332 | return ACPI_HANDLE(dev) == (acpi_handle)data; |
| 333 | } |
| 334 | |
Andy Shevchenko | c64ffff | 2017-07-17 17:13:28 +0300 | [diff] [blame] | 335 | static int i2c_acpi_find_match_device(struct device *dev, void *data) |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 336 | { |
| 337 | return ACPI_COMPANION(dev) == data; |
| 338 | } |
| 339 | |
| 340 | static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle) |
| 341 | { |
| 342 | struct device *dev; |
| 343 | |
| 344 | dev = bus_find_device(&i2c_bus_type, NULL, handle, |
Andy Shevchenko | c64ffff | 2017-07-17 17:13:28 +0300 | [diff] [blame] | 345 | i2c_acpi_find_match_adapter); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 346 | return dev ? i2c_verify_adapter(dev) : NULL; |
| 347 | } |
| 348 | |
| 349 | static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev) |
| 350 | { |
| 351 | struct device *dev; |
| 352 | |
Andy Shevchenko | c64ffff | 2017-07-17 17:13:28 +0300 | [diff] [blame] | 353 | dev = bus_find_device(&i2c_bus_type, NULL, adev, |
| 354 | i2c_acpi_find_match_device); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 355 | return dev ? i2c_verify_client(dev) : NULL; |
| 356 | } |
| 357 | |
| 358 | static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value, |
| 359 | void *arg) |
| 360 | { |
| 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; |
| 366 | |
| 367 | switch (value) { |
| 368 | case ACPI_RECONFIG_DEVICE_ADD: |
| 369 | if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle)) |
| 370 | break; |
| 371 | |
| 372 | adapter = i2c_acpi_find_adapter_by_handle(adapter_handle); |
| 373 | if (!adapter) |
| 374 | break; |
| 375 | |
| 376 | i2c_acpi_register_device(adapter, adev, &info); |
| 377 | break; |
| 378 | case ACPI_RECONFIG_DEVICE_REMOVE: |
| 379 | if (!acpi_device_enumerated(adev)) |
| 380 | break; |
| 381 | |
| 382 | client = i2c_acpi_find_client_by_adev(adev); |
| 383 | if (!client) |
| 384 | break; |
| 385 | |
| 386 | i2c_unregister_device(client); |
| 387 | put_device(&client->dev); |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | return NOTIFY_OK; |
| 392 | } |
| 393 | |
| 394 | struct notifier_block i2c_acpi_notifier = { |
| 395 | .notifier_call = i2c_acpi_notify, |
| 396 | }; |
| 397 | |
| 398 | /** |
| 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) |
| 403 | * Context: can sleep |
| 404 | * |
| 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. |
| 409 | * |
| 410 | * Also see i2c_new_device, which this function calls to create the i2c-client. |
| 411 | * |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 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. |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 414 | */ |
| 415 | struct i2c_client *i2c_acpi_new_device(struct device *dev, int index, |
| 416 | struct i2c_board_info *info) |
| 417 | { |
| 418 | struct i2c_acpi_lookup lookup; |
| 419 | struct i2c_adapter *adapter; |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 420 | struct i2c_client *client; |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 421 | struct acpi_device *adev; |
| 422 | LIST_HEAD(resource_list); |
| 423 | int ret; |
| 424 | |
| 425 | adev = ACPI_COMPANION(dev); |
| 426 | if (!adev) |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 427 | return ERR_PTR(-EINVAL); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 428 | |
| 429 | memset(&lookup, 0, sizeof(lookup)); |
| 430 | lookup.info = info; |
| 431 | lookup.device_handle = acpi_device_handle(adev); |
| 432 | lookup.index = index; |
| 433 | |
| 434 | ret = acpi_dev_get_resources(adev, &resource_list, |
| 435 | i2c_acpi_fill_info, &lookup); |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 436 | if (ret < 0) |
| 437 | return ERR_PTR(ret); |
| 438 | |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 439 | acpi_dev_free_resource_list(&resource_list); |
| 440 | |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 441 | if (!info->addr) |
| 442 | return ERR_PTR(-EADDRNOTAVAIL); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 443 | |
| 444 | adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle); |
| 445 | if (!adapter) |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 446 | return ERR_PTR(-EPROBE_DEFER); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 447 | |
Andy Shevchenko | 2dea645 | 2018-11-28 13:45:25 +0200 | [diff] [blame] | 448 | client = i2c_new_device(adapter, info); |
| 449 | if (!client) |
| 450 | return ERR_PTR(-ENODEV); |
| 451 | |
| 452 | return client; |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 453 | } |
| 454 | EXPORT_SYMBOL_GPL(i2c_acpi_new_device); |
| 455 | |
| 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) |
| 459 | { |
| 460 | |
| 461 | struct i2c_msg msgs[2]; |
| 462 | int ret; |
| 463 | u8 *buffer; |
| 464 | |
| 465 | buffer = kzalloc(data_len, GFP_KERNEL); |
| 466 | if (!buffer) |
| 467 | return AE_NO_MEMORY; |
| 468 | |
| 469 | msgs[0].addr = client->addr; |
| 470 | msgs[0].flags = client->flags; |
| 471 | msgs[0].len = 1; |
| 472 | msgs[0].buf = &cmd; |
| 473 | |
| 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; |
| 478 | |
| 479 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
Hans de Goede | 7781eda | 2018-04-22 19:58:00 +0200 | [diff] [blame] | 480 | if (ret < 0) { |
| 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); |
| 485 | else |
| 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); |
Hans de Goede | 0a30446 | 2018-08-12 12:53:21 +0200 | [diff] [blame] | 488 | /* 2 transfers must have completed successfully */ |
| 489 | } else if (ret == 2) { |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 490 | memcpy(data, buffer, data_len); |
Hans de Goede | 0a30446 | 2018-08-12 12:53:21 +0200 | [diff] [blame] | 491 | ret = 0; |
| 492 | } else { |
| 493 | ret = -EIO; |
Hans de Goede | 7781eda | 2018-04-22 19:58:00 +0200 | [diff] [blame] | 494 | } |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 495 | |
| 496 | kfree(buffer); |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int acpi_gsb_i2c_write_bytes(struct i2c_client *client, |
| 501 | u8 cmd, u8 *data, u8 data_len) |
| 502 | { |
| 503 | |
| 504 | struct i2c_msg msgs[1]; |
| 505 | u8 *buffer; |
| 506 | int ret = AE_OK; |
| 507 | |
| 508 | buffer = kzalloc(data_len + 1, GFP_KERNEL); |
| 509 | if (!buffer) |
| 510 | return AE_NO_MEMORY; |
| 511 | |
| 512 | buffer[0] = cmd; |
| 513 | memcpy(buffer + 1, data, data_len); |
| 514 | |
| 515 | msgs[0].addr = client->addr; |
| 516 | msgs[0].flags = client->flags; |
| 517 | msgs[0].len = data_len + 1; |
| 518 | msgs[0].buf = buffer; |
| 519 | |
| 520 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 521 | |
| 522 | kfree(buffer); |
Hans de Goede | c463a15 | 2018-08-12 12:53:20 +0200 | [diff] [blame] | 523 | |
| 524 | if (ret < 0) { |
| 525 | dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret); |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | /* 1 transfer must have completed successfully */ |
| 530 | return (ret == 1) ? 0 : -EIO; |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static acpi_status |
| 534 | i2c_acpi_space_handler(u32 function, acpi_physical_address command, |
| 535 | u32 bits, u64 *value64, |
| 536 | void *handler_context, void *region_context) |
| 537 | { |
| 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; |
| 547 | acpi_status ret; |
| 548 | int status; |
| 549 | |
| 550 | ret = acpi_buffer_to_resource(info->connection, info->length, &ares); |
| 551 | if (ACPI_FAILURE(ret)) |
| 552 | return ret; |
| 553 | |
| 554 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 555 | if (!client) { |
| 556 | ret = AE_NO_MEMORY; |
| 557 | goto err; |
| 558 | } |
| 559 | |
Andy Shevchenko | 0d5102f | 2018-11-28 13:45:29 +0200 | [diff] [blame] | 560 | if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) { |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 561 | ret = AE_BAD_PARAMETER; |
| 562 | goto err; |
| 563 | } |
| 564 | |
| 565 | client->adapter = adapter; |
| 566 | client->addr = sb->slave_address; |
| 567 | |
| 568 | if (sb->access_mode == ACPI_I2C_10BIT_MODE) |
| 569 | client->flags |= I2C_CLIENT_TEN; |
| 570 | |
| 571 | switch (accessor_type) { |
| 572 | case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV: |
| 573 | if (action == ACPI_READ) { |
| 574 | status = i2c_smbus_read_byte(client); |
| 575 | if (status >= 0) { |
| 576 | gsb->bdata = status; |
| 577 | status = 0; |
| 578 | } |
| 579 | } else { |
| 580 | status = i2c_smbus_write_byte(client, gsb->bdata); |
| 581 | } |
| 582 | break; |
| 583 | |
| 584 | case ACPI_GSB_ACCESS_ATTRIB_BYTE: |
| 585 | if (action == ACPI_READ) { |
| 586 | status = i2c_smbus_read_byte_data(client, command); |
| 587 | if (status >= 0) { |
| 588 | gsb->bdata = status; |
| 589 | status = 0; |
| 590 | } |
| 591 | } else { |
| 592 | status = i2c_smbus_write_byte_data(client, command, |
| 593 | gsb->bdata); |
| 594 | } |
| 595 | break; |
| 596 | |
| 597 | case ACPI_GSB_ACCESS_ATTRIB_WORD: |
| 598 | if (action == ACPI_READ) { |
| 599 | status = i2c_smbus_read_word_data(client, command); |
| 600 | if (status >= 0) { |
| 601 | gsb->wdata = status; |
| 602 | status = 0; |
| 603 | } |
| 604 | } else { |
| 605 | status = i2c_smbus_write_word_data(client, command, |
| 606 | gsb->wdata); |
| 607 | } |
| 608 | break; |
| 609 | |
| 610 | case ACPI_GSB_ACCESS_ATTRIB_BLOCK: |
| 611 | if (action == ACPI_READ) { |
| 612 | status = i2c_smbus_read_block_data(client, command, |
| 613 | gsb->data); |
| 614 | if (status >= 0) { |
| 615 | gsb->len = status; |
| 616 | status = 0; |
| 617 | } |
| 618 | } else { |
| 619 | status = i2c_smbus_write_block_data(client, command, |
| 620 | gsb->len, gsb->data); |
| 621 | } |
| 622 | break; |
| 623 | |
| 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); |
Wolfram Sang | 53f8f7c | 2017-05-23 16:22:23 +0200 | [diff] [blame] | 628 | } else { |
| 629 | status = acpi_gsb_i2c_write_bytes(client, command, |
| 630 | gsb->data, info->access_length); |
| 631 | } |
| 632 | break; |
| 633 | |
| 634 | default: |
| 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; |
| 638 | goto err; |
| 639 | } |
| 640 | |
| 641 | gsb->status = status; |
| 642 | |
| 643 | err: |
| 644 | kfree(client); |
| 645 | ACPI_FREE(ares); |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) |
| 651 | { |
| 652 | acpi_handle handle; |
| 653 | struct i2c_acpi_handler_data *data; |
| 654 | acpi_status status; |
| 655 | |
| 656 | if (!adapter->dev.parent) |
| 657 | return -ENODEV; |
| 658 | |
| 659 | handle = ACPI_HANDLE(adapter->dev.parent); |
| 660 | |
| 661 | if (!handle) |
| 662 | return -ENODEV; |
| 663 | |
| 664 | data = kzalloc(sizeof(struct i2c_acpi_handler_data), |
| 665 | GFP_KERNEL); |
| 666 | if (!data) |
| 667 | return -ENOMEM; |
| 668 | |
| 669 | data->adapter = adapter; |
| 670 | status = acpi_bus_attach_private_data(handle, (void *)data); |
| 671 | if (ACPI_FAILURE(status)) { |
| 672 | kfree(data); |
| 673 | return -ENOMEM; |
| 674 | } |
| 675 | |
| 676 | status = acpi_install_address_space_handler(handle, |
| 677 | ACPI_ADR_SPACE_GSBUS, |
| 678 | &i2c_acpi_space_handler, |
| 679 | NULL, |
| 680 | data); |
| 681 | if (ACPI_FAILURE(status)) { |
| 682 | dev_err(&adapter->dev, "Error installing i2c space handler\n"); |
| 683 | acpi_bus_detach_private_data(handle); |
| 684 | kfree(data); |
| 685 | return -ENOMEM; |
| 686 | } |
| 687 | |
| 688 | acpi_walk_dep_device_list(handle); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) |
| 693 | { |
| 694 | acpi_handle handle; |
| 695 | struct i2c_acpi_handler_data *data; |
| 696 | acpi_status status; |
| 697 | |
| 698 | if (!adapter->dev.parent) |
| 699 | return; |
| 700 | |
| 701 | handle = ACPI_HANDLE(adapter->dev.parent); |
| 702 | |
| 703 | if (!handle) |
| 704 | return; |
| 705 | |
| 706 | acpi_remove_address_space_handler(handle, |
| 707 | ACPI_ADR_SPACE_GSBUS, |
| 708 | &i2c_acpi_space_handler); |
| 709 | |
| 710 | status = acpi_bus_get_private_data(handle, (void **)&data); |
| 711 | if (ACPI_SUCCESS(status)) |
| 712 | kfree(data); |
| 713 | |
| 714 | acpi_bus_detach_private_data(handle); |
| 715 | } |
| 716 | #endif /* CONFIG_ACPI_I2C_OPREGION */ |