i2c-core-acpi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. #include <linux/acpi.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include "i2c-core.h"
  19. struct i2c_acpi_handler_data {
  20. struct acpi_connection_info info;
  21. struct i2c_adapter *adapter;
  22. };
  23. struct gsb_buffer {
  24. u8 status;
  25. u8 len;
  26. union {
  27. u16 wdata;
  28. u8 bdata;
  29. u8 data[0];
  30. };
  31. } __packed;
  32. struct i2c_acpi_lookup {
  33. struct i2c_board_info *info;
  34. acpi_handle adapter_handle;
  35. acpi_handle device_handle;
  36. acpi_handle search_handle;
  37. int n;
  38. int index;
  39. u32 speed;
  40. u32 min_speed;
  41. u32 force_speed;
  42. };
  43. static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
  44. {
  45. struct i2c_acpi_lookup *lookup = data;
  46. struct i2c_board_info *info = lookup->info;
  47. struct acpi_resource_i2c_serialbus *sb;
  48. acpi_status status;
  49. if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  50. return 1;
  51. sb = &ares->data.i2c_serial_bus;
  52. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
  53. return 1;
  54. if (lookup->index != -1 && lookup->n++ != lookup->index)
  55. return 1;
  56. status = acpi_get_handle(lookup->device_handle,
  57. sb->resource_source.string_ptr,
  58. &lookup->adapter_handle);
  59. if (!ACPI_SUCCESS(status))
  60. return 1;
  61. info->addr = sb->slave_address;
  62. lookup->speed = sb->connection_speed;
  63. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  64. info->flags |= I2C_CLIENT_TEN;
  65. return 1;
  66. }
  67. static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
  68. /*
  69. * ACPI video acpi_devices, which are handled by the acpi-video driver
  70. * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
  71. */
  72. { ACPI_VIDEO_HID, 0 },
  73. {}
  74. };
  75. static int i2c_acpi_do_lookup(struct acpi_device *adev,
  76. struct i2c_acpi_lookup *lookup)
  77. {
  78. struct i2c_board_info *info = lookup->info;
  79. struct list_head resource_list;
  80. int ret;
  81. if (acpi_bus_get_status(adev) || !adev->status.present ||
  82. acpi_device_enumerated(adev))
  83. return -EINVAL;
  84. if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
  85. return -ENODEV;
  86. memset(info, 0, sizeof(*info));
  87. lookup->device_handle = acpi_device_handle(adev);
  88. /* Look up for I2cSerialBus resource */
  89. INIT_LIST_HEAD(&resource_list);
  90. ret = acpi_dev_get_resources(adev, &resource_list,
  91. i2c_acpi_fill_info, lookup);
  92. acpi_dev_free_resource_list(&resource_list);
  93. if (ret < 0 || !info->addr)
  94. return -EINVAL;
  95. return 0;
  96. }
  97. static int i2c_acpi_get_info(struct acpi_device *adev,
  98. struct i2c_board_info *info,
  99. struct i2c_adapter *adapter,
  100. acpi_handle *adapter_handle)
  101. {
  102. struct list_head resource_list;
  103. struct resource_entry *entry;
  104. struct i2c_acpi_lookup lookup;
  105. int ret;
  106. memset(&lookup, 0, sizeof(lookup));
  107. lookup.info = info;
  108. lookup.index = -1;
  109. ret = i2c_acpi_do_lookup(adev, &lookup);
  110. if (ret)
  111. return ret;
  112. if (adapter) {
  113. /* The adapter must match the one in I2cSerialBus() connector */
  114. if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
  115. return -ENODEV;
  116. } else {
  117. struct acpi_device *adapter_adev;
  118. /* The adapter must be present */
  119. if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
  120. return -ENODEV;
  121. if (acpi_bus_get_status(adapter_adev) ||
  122. !adapter_adev->status.present)
  123. return -ENODEV;
  124. }
  125. info->fwnode = acpi_fwnode_handle(adev);
  126. if (adapter_handle)
  127. *adapter_handle = lookup.adapter_handle;
  128. /* Then fill IRQ number if any */
  129. INIT_LIST_HEAD(&resource_list);
  130. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  131. if (ret < 0)
  132. return -EINVAL;
  133. resource_list_for_each_entry(entry, &resource_list) {
  134. if (resource_type(entry->res) == IORESOURCE_IRQ) {
  135. info->irq = entry->res->start;
  136. break;
  137. }
  138. }
  139. acpi_dev_free_resource_list(&resource_list);
  140. acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
  141. sizeof(info->type));
  142. return 0;
  143. }
  144. static void i2c_acpi_register_device(struct i2c_adapter *adapter,
  145. struct acpi_device *adev,
  146. struct i2c_board_info *info)
  147. {
  148. adev->power.flags.ignore_parent = true;
  149. acpi_device_set_enumerated(adev);
  150. if (!i2c_new_device(adapter, info)) {
  151. adev->power.flags.ignore_parent = false;
  152. dev_err(&adapter->dev,
  153. "failed to add I2C device %s from ACPI\n",
  154. dev_name(&adev->dev));
  155. }
  156. }
  157. static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
  158. void *data, void **return_value)
  159. {
  160. struct i2c_adapter *adapter = data;
  161. struct acpi_device *adev;
  162. struct i2c_board_info info;
  163. if (acpi_bus_get_device(handle, &adev))
  164. return AE_OK;
  165. if (i2c_acpi_get_info(adev, &info, adapter, NULL))
  166. return AE_OK;
  167. i2c_acpi_register_device(adapter, adev, &info);
  168. return AE_OK;
  169. }
  170. #define I2C_ACPI_MAX_SCAN_DEPTH 32
  171. /**
  172. * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
  173. * @adap: pointer to adapter
  174. *
  175. * Enumerate all I2C slave devices behind this adapter by walking the ACPI
  176. * namespace. When a device is found it will be added to the Linux device
  177. * model and bound to the corresponding ACPI handle.
  178. */
  179. void i2c_acpi_register_devices(struct i2c_adapter *adap)
  180. {
  181. acpi_status status;
  182. acpi_handle handle;
  183. if (!has_acpi_companion(&adap->dev))
  184. return;
  185. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  186. I2C_ACPI_MAX_SCAN_DEPTH,
  187. i2c_acpi_add_device, NULL,
  188. adap, NULL);
  189. if (ACPI_FAILURE(status))
  190. dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
  191. if (!adap->dev.parent)
  192. return;
  193. handle = ACPI_HANDLE(adap->dev.parent);
  194. if (!handle)
  195. return;
  196. acpi_walk_dep_device_list(handle);
  197. }
  198. const struct acpi_device_id *
  199. i2c_acpi_match_device(const struct acpi_device_id *matches,
  200. struct i2c_client *client)
  201. {
  202. if (!(client && matches))
  203. return NULL;
  204. return acpi_match_device(matches, &client->dev);
  205. }
  206. static const struct acpi_device_id i2c_acpi_force_400khz_device_ids[] = {
  207. /*
  208. * These Silead touchscreen controllers only work at 400KHz, for
  209. * some reason they do not work at 100KHz. On some devices the ACPI
  210. * tables list another device at their bus as only being capable
  211. * of 100KHz, testing has shown that these other devices work fine
  212. * at 400KHz (as can be expected of any recent i2c hw) so we force
  213. * the speed of the bus to 400 KHz if a Silead device is present.
  214. */
  215. { "MSSL1680", 0 },
  216. {}
  217. };
  218. static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
  219. void *data, void **return_value)
  220. {
  221. struct i2c_acpi_lookup *lookup = data;
  222. struct acpi_device *adev;
  223. if (acpi_bus_get_device(handle, &adev))
  224. return AE_OK;
  225. if (i2c_acpi_do_lookup(adev, lookup))
  226. return AE_OK;
  227. if (lookup->search_handle != lookup->adapter_handle)
  228. return AE_OK;
  229. if (lookup->speed <= lookup->min_speed)
  230. lookup->min_speed = lookup->speed;
  231. if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
  232. lookup->force_speed = 400000;
  233. return AE_OK;
  234. }
  235. /**
  236. * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
  237. * @dev: The device owning the bus
  238. *
  239. * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
  240. * devices connected to this bus and use the speed of slowest device.
  241. *
  242. * Returns the speed in Hz or zero
  243. */
  244. u32 i2c_acpi_find_bus_speed(struct device *dev)
  245. {
  246. struct i2c_acpi_lookup lookup;
  247. struct i2c_board_info dummy;
  248. acpi_status status;
  249. if (!has_acpi_companion(dev))
  250. return 0;
  251. memset(&lookup, 0, sizeof(lookup));
  252. lookup.search_handle = ACPI_HANDLE(dev);
  253. lookup.min_speed = UINT_MAX;
  254. lookup.info = &dummy;
  255. lookup.index = -1;
  256. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  257. I2C_ACPI_MAX_SCAN_DEPTH,
  258. i2c_acpi_lookup_speed, NULL,
  259. &lookup, NULL);
  260. if (ACPI_FAILURE(status)) {
  261. dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
  262. return 0;
  263. }
  264. if (lookup.force_speed) {
  265. if (lookup.force_speed != lookup.min_speed)
  266. dev_warn(dev, FW_BUG "DSDT uses known not-working I2C bus speed %d, forcing it to %d\n",
  267. lookup.min_speed, lookup.force_speed);
  268. return lookup.force_speed;
  269. } else if (lookup.min_speed != UINT_MAX) {
  270. return lookup.min_speed;
  271. } else {
  272. return 0;
  273. }
  274. }
  275. EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
  276. static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
  277. {
  278. struct i2c_adapter *adapter = i2c_verify_adapter(dev);
  279. if (!adapter)
  280. return 0;
  281. return ACPI_HANDLE(dev) == (acpi_handle)data;
  282. }
  283. static int i2c_acpi_find_match_device(struct device *dev, void *data)
  284. {
  285. return ACPI_COMPANION(dev) == data;
  286. }
  287. static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
  288. {
  289. struct device *dev;
  290. dev = bus_find_device(&i2c_bus_type, NULL, handle,
  291. i2c_acpi_find_match_adapter);
  292. return dev ? i2c_verify_adapter(dev) : NULL;
  293. }
  294. static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
  295. {
  296. struct device *dev;
  297. struct i2c_client *client;
  298. dev = bus_find_device(&i2c_bus_type, NULL, adev,
  299. i2c_acpi_find_match_device);
  300. if (!dev)
  301. return NULL;
  302. client = i2c_verify_client(dev);
  303. if (!client)
  304. put_device(dev);
  305. return client;
  306. }
  307. static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
  308. void *arg)
  309. {
  310. struct acpi_device *adev = arg;
  311. struct i2c_board_info info;
  312. acpi_handle adapter_handle;
  313. struct i2c_adapter *adapter;
  314. struct i2c_client *client;
  315. switch (value) {
  316. case ACPI_RECONFIG_DEVICE_ADD:
  317. if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
  318. break;
  319. adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
  320. if (!adapter)
  321. break;
  322. i2c_acpi_register_device(adapter, adev, &info);
  323. break;
  324. case ACPI_RECONFIG_DEVICE_REMOVE:
  325. if (!acpi_device_enumerated(adev))
  326. break;
  327. client = i2c_acpi_find_client_by_adev(adev);
  328. if (!client)
  329. break;
  330. i2c_unregister_device(client);
  331. put_device(&client->dev);
  332. break;
  333. }
  334. return NOTIFY_OK;
  335. }
  336. struct notifier_block i2c_acpi_notifier = {
  337. .notifier_call = i2c_acpi_notify,
  338. };
  339. /**
  340. * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
  341. * @dev: Device owning the ACPI resources to get the client from
  342. * @index: Index of ACPI resource to get
  343. * @info: describes the I2C device; note this is modified (addr gets set)
  344. * Context: can sleep
  345. *
  346. * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
  347. * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
  348. * resources, in that case this function can be used to create an i2c-client
  349. * for other I2cSerialBus resources in the Current Resource Settings table.
  350. *
  351. * Also see i2c_new_device, which this function calls to create the i2c-client.
  352. *
  353. * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
  354. */
  355. struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
  356. struct i2c_board_info *info)
  357. {
  358. struct i2c_acpi_lookup lookup;
  359. struct i2c_adapter *adapter;
  360. struct acpi_device *adev;
  361. LIST_HEAD(resource_list);
  362. int ret;
  363. adev = ACPI_COMPANION(dev);
  364. if (!adev)
  365. return NULL;
  366. memset(&lookup, 0, sizeof(lookup));
  367. lookup.info = info;
  368. lookup.device_handle = acpi_device_handle(adev);
  369. lookup.index = index;
  370. ret = acpi_dev_get_resources(adev, &resource_list,
  371. i2c_acpi_fill_info, &lookup);
  372. acpi_dev_free_resource_list(&resource_list);
  373. if (ret < 0 || !info->addr)
  374. return NULL;
  375. adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
  376. if (!adapter)
  377. return NULL;
  378. return i2c_new_device(adapter, info);
  379. }
  380. EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
  381. #ifdef CONFIG_ACPI_I2C_OPREGION
  382. static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
  383. u8 cmd, u8 *data, u8 data_len)
  384. {
  385. struct i2c_msg msgs[2];
  386. int ret;
  387. u8 *buffer;
  388. buffer = kzalloc(data_len, GFP_KERNEL);
  389. if (!buffer)
  390. return AE_NO_MEMORY;
  391. msgs[0].addr = client->addr;
  392. msgs[0].flags = client->flags;
  393. msgs[0].len = 1;
  394. msgs[0].buf = &cmd;
  395. msgs[1].addr = client->addr;
  396. msgs[1].flags = client->flags | I2C_M_RD;
  397. msgs[1].len = data_len;
  398. msgs[1].buf = buffer;
  399. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  400. if (ret < 0)
  401. dev_err(&client->adapter->dev, "i2c read failed\n");
  402. else
  403. memcpy(data, buffer, data_len);
  404. kfree(buffer);
  405. return ret;
  406. }
  407. static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
  408. u8 cmd, u8 *data, u8 data_len)
  409. {
  410. struct i2c_msg msgs[1];
  411. u8 *buffer;
  412. int ret = AE_OK;
  413. buffer = kzalloc(data_len + 1, GFP_KERNEL);
  414. if (!buffer)
  415. return AE_NO_MEMORY;
  416. buffer[0] = cmd;
  417. memcpy(buffer + 1, data, data_len);
  418. msgs[0].addr = client->addr;
  419. msgs[0].flags = client->flags;
  420. msgs[0].len = data_len + 1;
  421. msgs[0].buf = buffer;
  422. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  423. kfree(buffer);
  424. if (ret < 0) {
  425. dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
  426. return ret;
  427. }
  428. /* 1 transfer must have completed successfully */
  429. return (ret == 1) ? 0 : -EIO;
  430. }
  431. static acpi_status
  432. i2c_acpi_space_handler(u32 function, acpi_physical_address command,
  433. u32 bits, u64 *value64,
  434. void *handler_context, void *region_context)
  435. {
  436. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  437. struct i2c_acpi_handler_data *data = handler_context;
  438. struct acpi_connection_info *info = &data->info;
  439. struct acpi_resource_i2c_serialbus *sb;
  440. struct i2c_adapter *adapter = data->adapter;
  441. struct i2c_client *client;
  442. struct acpi_resource *ares;
  443. u32 accessor_type = function >> 16;
  444. u8 action = function & ACPI_IO_MASK;
  445. acpi_status ret;
  446. int status;
  447. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  448. if (ACPI_FAILURE(ret))
  449. return ret;
  450. client = kzalloc(sizeof(*client), GFP_KERNEL);
  451. if (!client) {
  452. ret = AE_NO_MEMORY;
  453. goto err;
  454. }
  455. if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  456. ret = AE_BAD_PARAMETER;
  457. goto err;
  458. }
  459. sb = &ares->data.i2c_serial_bus;
  460. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  461. ret = AE_BAD_PARAMETER;
  462. goto err;
  463. }
  464. client->adapter = adapter;
  465. client->addr = sb->slave_address;
  466. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  467. client->flags |= I2C_CLIENT_TEN;
  468. switch (accessor_type) {
  469. case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
  470. if (action == ACPI_READ) {
  471. status = i2c_smbus_read_byte(client);
  472. if (status >= 0) {
  473. gsb->bdata = status;
  474. status = 0;
  475. }
  476. } else {
  477. status = i2c_smbus_write_byte(client, gsb->bdata);
  478. }
  479. break;
  480. case ACPI_GSB_ACCESS_ATTRIB_BYTE:
  481. if (action == ACPI_READ) {
  482. status = i2c_smbus_read_byte_data(client, command);
  483. if (status >= 0) {
  484. gsb->bdata = status;
  485. status = 0;
  486. }
  487. } else {
  488. status = i2c_smbus_write_byte_data(client, command,
  489. gsb->bdata);
  490. }
  491. break;
  492. case ACPI_GSB_ACCESS_ATTRIB_WORD:
  493. if (action == ACPI_READ) {
  494. status = i2c_smbus_read_word_data(client, command);
  495. if (status >= 0) {
  496. gsb->wdata = status;
  497. status = 0;
  498. }
  499. } else {
  500. status = i2c_smbus_write_word_data(client, command,
  501. gsb->wdata);
  502. }
  503. break;
  504. case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
  505. if (action == ACPI_READ) {
  506. status = i2c_smbus_read_block_data(client, command,
  507. gsb->data);
  508. if (status >= 0) {
  509. gsb->len = status;
  510. status = 0;
  511. }
  512. } else {
  513. status = i2c_smbus_write_block_data(client, command,
  514. gsb->len, gsb->data);
  515. }
  516. break;
  517. case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
  518. if (action == ACPI_READ) {
  519. status = acpi_gsb_i2c_read_bytes(client, command,
  520. gsb->data, info->access_length);
  521. if (status > 0)
  522. status = 0;
  523. } else {
  524. status = acpi_gsb_i2c_write_bytes(client, command,
  525. gsb->data, info->access_length);
  526. }
  527. break;
  528. default:
  529. dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
  530. accessor_type, client->addr);
  531. ret = AE_BAD_PARAMETER;
  532. goto err;
  533. }
  534. gsb->status = status;
  535. err:
  536. kfree(client);
  537. ACPI_FREE(ares);
  538. return ret;
  539. }
  540. int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
  541. {
  542. acpi_handle handle;
  543. struct i2c_acpi_handler_data *data;
  544. acpi_status status;
  545. if (!adapter->dev.parent)
  546. return -ENODEV;
  547. handle = ACPI_HANDLE(adapter->dev.parent);
  548. if (!handle)
  549. return -ENODEV;
  550. data = kzalloc(sizeof(struct i2c_acpi_handler_data),
  551. GFP_KERNEL);
  552. if (!data)
  553. return -ENOMEM;
  554. data->adapter = adapter;
  555. status = acpi_bus_attach_private_data(handle, (void *)data);
  556. if (ACPI_FAILURE(status)) {
  557. kfree(data);
  558. return -ENOMEM;
  559. }
  560. status = acpi_install_address_space_handler(handle,
  561. ACPI_ADR_SPACE_GSBUS,
  562. &i2c_acpi_space_handler,
  563. NULL,
  564. data);
  565. if (ACPI_FAILURE(status)) {
  566. dev_err(&adapter->dev, "Error installing i2c space handler\n");
  567. acpi_bus_detach_private_data(handle);
  568. kfree(data);
  569. return -ENOMEM;
  570. }
  571. return 0;
  572. }
  573. void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
  574. {
  575. acpi_handle handle;
  576. struct i2c_acpi_handler_data *data;
  577. acpi_status status;
  578. if (!adapter->dev.parent)
  579. return;
  580. handle = ACPI_HANDLE(adapter->dev.parent);
  581. if (!handle)
  582. return;
  583. acpi_remove_address_space_handler(handle,
  584. ACPI_ADR_SPACE_GSBUS,
  585. &i2c_acpi_space_handler);
  586. status = acpi_bus_get_private_data(handle, (void **)&data);
  587. if (ACPI_SUCCESS(status))
  588. kfree(data);
  589. acpi_bus_detach_private_data(handle);
  590. }
  591. #endif /* CONFIG_ACPI_I2C_OPREGION */