i2c-core-acpi.c 16 KB

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