i2c-core-acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. 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 const struct acpi_device_id i2c_acpi_force_100khz_device_ids[] = {
  290. /*
  291. * When a 400KHz freq is used on this model of ELAN touchpad in Linux,
  292. * excessive smoothing (similar to when the touchpad's firmware detects
  293. * a noisy signal) is sometimes applied. As some devices' (e.g, Lenovo
  294. * V15 G4) ACPI tables specify a 400KHz frequency for this device and
  295. * some I2C busses (e.g, Designware I2C) default to a 400KHz freq,
  296. * force the speed to 100KHz as a workaround.
  297. *
  298. * For future investigation: This problem may be related to the default
  299. * HCNT/LCNT values given by some busses' drivers, because they are not
  300. * specified in the aforementioned devices' ACPI tables, and because
  301. * the device works without issues on Windows at what is expected to be
  302. * a 400KHz frequency. The root cause of the issue is not known.
  303. */
  304. { "DLL0945", 0 },
  305. { "ELAN06FA", 0 },
  306. {}
  307. };
  308. static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
  309. void *data, void **return_value)
  310. {
  311. struct i2c_acpi_lookup *lookup = data;
  312. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  313. if (!adev || i2c_acpi_do_lookup(adev, lookup))
  314. return AE_OK;
  315. if (lookup->search_handle != lookup->adapter_handle)
  316. return AE_OK;
  317. if (lookup->speed <= lookup->min_speed)
  318. lookup->min_speed = lookup->speed;
  319. if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
  320. lookup->force_speed = I2C_MAX_FAST_MODE_FREQ;
  321. if (acpi_match_device_ids(adev, i2c_acpi_force_100khz_device_ids) == 0)
  322. lookup->force_speed = I2C_MAX_STANDARD_MODE_FREQ;
  323. return AE_OK;
  324. }
  325. /**
  326. * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
  327. * @dev: The device owning the bus
  328. *
  329. * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
  330. * devices connected to this bus and use the speed of slowest device.
  331. *
  332. * Returns the speed in Hz or zero
  333. */
  334. u32 i2c_acpi_find_bus_speed(struct device *dev)
  335. {
  336. struct i2c_acpi_lookup lookup;
  337. struct i2c_board_info dummy;
  338. acpi_status status;
  339. if (!has_acpi_companion(dev))
  340. return 0;
  341. memset(&lookup, 0, sizeof(lookup));
  342. lookup.search_handle = ACPI_HANDLE(dev);
  343. lookup.min_speed = UINT_MAX;
  344. lookup.info = &dummy;
  345. lookup.index = -1;
  346. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  347. I2C_ACPI_MAX_SCAN_DEPTH,
  348. i2c_acpi_lookup_speed, NULL,
  349. &lookup, NULL);
  350. if (ACPI_FAILURE(status)) {
  351. dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
  352. return 0;
  353. }
  354. if (lookup.force_speed) {
  355. if (lookup.force_speed != lookup.min_speed)
  356. dev_warn(dev, FW_BUG "DSDT uses known not-working I2C bus speed %d, forcing it to %d\n",
  357. lookup.min_speed, lookup.force_speed);
  358. return lookup.force_speed;
  359. } else if (lookup.min_speed != UINT_MAX) {
  360. return lookup.min_speed;
  361. } else {
  362. return 0;
  363. }
  364. }
  365. EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
  366. struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
  367. {
  368. struct i2c_adapter *adapter;
  369. struct device *dev;
  370. dev = bus_find_device(&i2c_bus_type, NULL, handle, device_match_acpi_handle);
  371. if (!dev)
  372. return NULL;
  373. adapter = i2c_verify_adapter(dev);
  374. if (!adapter)
  375. put_device(dev);
  376. return adapter;
  377. }
  378. EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
  379. static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
  380. {
  381. return i2c_find_device_by_fwnode(acpi_fwnode_handle(adev));
  382. }
  383. static struct i2c_adapter *i2c_acpi_find_adapter_by_adev(struct acpi_device *adev)
  384. {
  385. return i2c_find_adapter_by_fwnode(acpi_fwnode_handle(adev));
  386. }
  387. static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
  388. void *arg)
  389. {
  390. struct acpi_device *adev = arg;
  391. struct i2c_board_info info;
  392. acpi_handle adapter_handle;
  393. struct i2c_adapter *adapter;
  394. struct i2c_client *client;
  395. switch (value) {
  396. case ACPI_RECONFIG_DEVICE_ADD:
  397. if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
  398. break;
  399. adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
  400. if (!adapter)
  401. break;
  402. i2c_acpi_register_device(adapter, adev, &info);
  403. put_device(&adapter->dev);
  404. break;
  405. case ACPI_RECONFIG_DEVICE_REMOVE:
  406. if (!acpi_device_enumerated(adev))
  407. break;
  408. client = i2c_acpi_find_client_by_adev(adev);
  409. if (client) {
  410. i2c_unregister_device(client);
  411. put_device(&client->dev);
  412. }
  413. adapter = i2c_acpi_find_adapter_by_adev(adev);
  414. if (adapter) {
  415. acpi_unbind_one(&adapter->dev);
  416. put_device(&adapter->dev);
  417. }
  418. break;
  419. }
  420. return NOTIFY_OK;
  421. }
  422. struct notifier_block i2c_acpi_notifier = {
  423. .notifier_call = i2c_acpi_notify,
  424. };
  425. /**
  426. * i2c_acpi_new_device_by_fwnode - Create i2c-client for the Nth I2cSerialBus resource
  427. * @fwnode: fwnode with the ACPI resources to get the client from
  428. * @index: Index of ACPI resource to get
  429. * @info: describes the I2C device; note this is modified (addr gets set)
  430. * Context: can sleep
  431. *
  432. * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
  433. * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
  434. * resources, in that case this function can be used to create an i2c-client
  435. * for other I2cSerialBus resources in the Current Resource Settings table.
  436. *
  437. * Also see i2c_new_client_device, which this function calls to create the
  438. * i2c-client.
  439. *
  440. * Returns a pointer to the new i2c-client, or error pointer in case of failure.
  441. * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
  442. */
  443. struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode,
  444. int index,
  445. struct i2c_board_info *info)
  446. {
  447. struct i2c_acpi_lookup lookup;
  448. struct i2c_adapter *adapter;
  449. struct acpi_device *adev;
  450. LIST_HEAD(resource_list);
  451. int ret;
  452. adev = to_acpi_device_node(fwnode);
  453. if (!adev)
  454. return ERR_PTR(-ENODEV);
  455. memset(&lookup, 0, sizeof(lookup));
  456. lookup.info = info;
  457. lookup.device_handle = acpi_device_handle(adev);
  458. lookup.index = index;
  459. ret = acpi_dev_get_resources(adev, &resource_list,
  460. i2c_acpi_fill_info, &lookup);
  461. if (ret < 0)
  462. return ERR_PTR(ret);
  463. acpi_dev_free_resource_list(&resource_list);
  464. if (!info->addr)
  465. return ERR_PTR(-EADDRNOTAVAIL);
  466. adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
  467. if (!adapter)
  468. return ERR_PTR(-EPROBE_DEFER);
  469. return i2c_new_client_device(adapter, info);
  470. }
  471. EXPORT_SYMBOL_GPL(i2c_acpi_new_device_by_fwnode);
  472. bool i2c_acpi_waive_d0_probe(struct device *dev)
  473. {
  474. struct i2c_driver *driver = to_i2c_driver(dev->driver);
  475. struct acpi_device *adev = ACPI_COMPANION(dev);
  476. return driver->flags & I2C_DRV_ACPI_WAIVE_D0_PROBE &&
  477. adev && adev->power.state_for_enumeration >= adev->power.state;
  478. }
  479. EXPORT_SYMBOL_GPL(i2c_acpi_waive_d0_probe);
  480. #ifdef CONFIG_ACPI_I2C_OPREGION
  481. static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
  482. u8 cmd, u8 *data, u8 data_len)
  483. {
  484. struct i2c_msg msgs[2];
  485. int ret;
  486. u8 *buffer;
  487. buffer = kzalloc(data_len, GFP_KERNEL);
  488. if (!buffer)
  489. return AE_NO_MEMORY;
  490. msgs[0].addr = client->addr;
  491. msgs[0].flags = client->flags;
  492. msgs[0].len = 1;
  493. msgs[0].buf = &cmd;
  494. msgs[1].addr = client->addr;
  495. msgs[1].flags = client->flags | I2C_M_RD;
  496. msgs[1].len = data_len;
  497. msgs[1].buf = buffer;
  498. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  499. if (ret < 0) {
  500. /* Getting a NACK is unfortunately normal with some DSTDs */
  501. if (ret == -EREMOTEIO)
  502. dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
  503. data_len, client->addr, cmd, ret);
  504. else
  505. dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
  506. data_len, client->addr, cmd, ret);
  507. /* 2 transfers must have completed successfully */
  508. } else if (ret == 2) {
  509. memcpy(data, buffer, data_len);
  510. ret = 0;
  511. } else {
  512. ret = -EIO;
  513. }
  514. kfree(buffer);
  515. return ret;
  516. }
  517. static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
  518. u8 cmd, u8 *data, u8 data_len)
  519. {
  520. struct i2c_msg msgs[1];
  521. u8 *buffer;
  522. int ret = AE_OK;
  523. buffer = kzalloc(data_len + 1, GFP_KERNEL);
  524. if (!buffer)
  525. return AE_NO_MEMORY;
  526. buffer[0] = cmd;
  527. memcpy(buffer + 1, data, data_len);
  528. msgs[0].addr = client->addr;
  529. msgs[0].flags = client->flags;
  530. msgs[0].len = data_len + 1;
  531. msgs[0].buf = buffer;
  532. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  533. kfree(buffer);
  534. if (ret < 0) {
  535. dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
  536. return ret;
  537. }
  538. /* 1 transfer must have completed successfully */
  539. return (ret == 1) ? 0 : -EIO;
  540. }
  541. static acpi_status
  542. i2c_acpi_space_handler(u32 function, acpi_physical_address command,
  543. u32 bits, u64 *value64,
  544. void *handler_context, void *region_context)
  545. {
  546. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  547. struct i2c_acpi_handler_data *data = handler_context;
  548. struct acpi_connection_info *info = &data->info;
  549. struct acpi_resource_i2c_serialbus *sb;
  550. struct i2c_adapter *adapter = data->adapter;
  551. struct i2c_client *client;
  552. struct acpi_resource *ares;
  553. u32 accessor_type = function >> 16;
  554. u8 action = function & ACPI_IO_MASK;
  555. acpi_status ret;
  556. int status;
  557. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  558. if (ACPI_FAILURE(ret))
  559. return ret;
  560. client = kzalloc(sizeof(*client), GFP_KERNEL);
  561. if (!client) {
  562. ret = AE_NO_MEMORY;
  563. goto err;
  564. }
  565. if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
  566. ret = AE_BAD_PARAMETER;
  567. goto err;
  568. }
  569. client->adapter = adapter;
  570. client->addr = sb->slave_address;
  571. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  572. client->flags |= I2C_CLIENT_TEN;
  573. switch (accessor_type) {
  574. case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
  575. if (action == ACPI_READ) {
  576. status = i2c_smbus_read_byte(client);
  577. if (status >= 0) {
  578. gsb->bdata = status;
  579. status = 0;
  580. }
  581. } else {
  582. status = i2c_smbus_write_byte(client, gsb->bdata);
  583. }
  584. break;
  585. case ACPI_GSB_ACCESS_ATTRIB_BYTE:
  586. if (action == ACPI_READ) {
  587. status = i2c_smbus_read_byte_data(client, command);
  588. if (status >= 0) {
  589. gsb->bdata = status;
  590. status = 0;
  591. }
  592. } else {
  593. status = i2c_smbus_write_byte_data(client, command,
  594. gsb->bdata);
  595. }
  596. break;
  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. case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
  610. if (action == ACPI_READ) {
  611. status = i2c_smbus_read_block_data(client, command,
  612. gsb->data);
  613. if (status >= 0) {
  614. gsb->len = status;
  615. status = 0;
  616. }
  617. } else {
  618. status = i2c_smbus_write_block_data(client, command,
  619. gsb->len, gsb->data);
  620. }
  621. break;
  622. case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
  623. if (action == ACPI_READ) {
  624. status = acpi_gsb_i2c_read_bytes(client, command,
  625. gsb->data, info->access_length);
  626. } else {
  627. status = acpi_gsb_i2c_write_bytes(client, command,
  628. gsb->data, info->access_length);
  629. }
  630. break;
  631. default:
  632. dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
  633. accessor_type, client->addr);
  634. ret = AE_BAD_PARAMETER;
  635. goto err;
  636. }
  637. gsb->status = status;
  638. err:
  639. kfree(client);
  640. ACPI_FREE(ares);
  641. return ret;
  642. }
  643. int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
  644. {
  645. acpi_handle handle;
  646. struct i2c_acpi_handler_data *data;
  647. acpi_status status;
  648. if (!adapter->dev.parent)
  649. return -ENODEV;
  650. handle = ACPI_HANDLE(adapter->dev.parent);
  651. if (!handle)
  652. return -ENODEV;
  653. data = kzalloc(sizeof(struct i2c_acpi_handler_data),
  654. GFP_KERNEL);
  655. if (!data)
  656. return -ENOMEM;
  657. data->adapter = adapter;
  658. status = acpi_bus_attach_private_data(handle, (void *)data);
  659. if (ACPI_FAILURE(status)) {
  660. kfree(data);
  661. return -ENOMEM;
  662. }
  663. status = acpi_install_address_space_handler(handle,
  664. ACPI_ADR_SPACE_GSBUS,
  665. &i2c_acpi_space_handler,
  666. NULL,
  667. data);
  668. if (ACPI_FAILURE(status)) {
  669. dev_err(&adapter->dev, "Error installing i2c space handler\n");
  670. acpi_bus_detach_private_data(handle);
  671. kfree(data);
  672. return -ENOMEM;
  673. }
  674. return 0;
  675. }
  676. void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
  677. {
  678. acpi_handle handle;
  679. struct i2c_acpi_handler_data *data;
  680. acpi_status status;
  681. if (!adapter->dev.parent)
  682. return;
  683. handle = ACPI_HANDLE(adapter->dev.parent);
  684. if (!handle)
  685. return;
  686. acpi_remove_address_space_handler(handle,
  687. ACPI_ADR_SPACE_GSBUS,
  688. &i2c_acpi_space_handler);
  689. status = acpi_bus_get_private_data(handle, (void **)&data);
  690. if (ACPI_SUCCESS(status))
  691. kfree(data);
  692. acpi_bus_detach_private_data(handle);
  693. }
  694. #endif /* CONFIG_ACPI_I2C_OPREGION */