i2c-core-acpi.c 20 KB

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