i2c-core-acpi.c 18 KB

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