i2c-acpi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * I2C ACPI code
  3. *
  4. * Copyright (C) 2014 Intel Corp
  5. *
  6. * Author: Lan Tianyu <tianyu.lan@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. #define pr_fmt(fmt) "I2C/ACPI : " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/acpi.h>
  23. struct acpi_i2c_handler_data {
  24. struct acpi_connection_info info;
  25. struct i2c_adapter *adapter;
  26. };
  27. struct gsb_buffer {
  28. u8 status;
  29. u8 len;
  30. union {
  31. u16 wdata;
  32. u8 bdata;
  33. u8 data[0];
  34. };
  35. } __packed;
  36. static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
  37. {
  38. struct i2c_board_info *info = data;
  39. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  40. struct acpi_resource_i2c_serialbus *sb;
  41. sb = &ares->data.i2c_serial_bus;
  42. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  43. info->addr = sb->slave_address;
  44. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  45. info->flags |= I2C_CLIENT_TEN;
  46. }
  47. } else if (info->irq < 0) {
  48. struct resource r;
  49. if (acpi_dev_resource_interrupt(ares, 0, &r))
  50. info->irq = r.start;
  51. }
  52. /* Tell the ACPI core to skip this resource */
  53. return 1;
  54. }
  55. static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
  56. void *data, void **return_value)
  57. {
  58. struct i2c_adapter *adapter = data;
  59. struct list_head resource_list;
  60. struct i2c_board_info info;
  61. struct acpi_device *adev;
  62. int ret;
  63. if (acpi_bus_get_device(handle, &adev))
  64. return AE_OK;
  65. if (acpi_bus_get_status(adev) || !adev->status.present)
  66. return AE_OK;
  67. memset(&info, 0, sizeof(info));
  68. info.acpi_node.companion = adev;
  69. info.irq = -1;
  70. INIT_LIST_HEAD(&resource_list);
  71. ret = acpi_dev_get_resources(adev, &resource_list,
  72. acpi_i2c_add_resource, &info);
  73. acpi_dev_free_resource_list(&resource_list);
  74. if (ret < 0 || !info.addr)
  75. return AE_OK;
  76. adev->power.flags.ignore_parent = true;
  77. strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
  78. if (!i2c_new_device(adapter, &info)) {
  79. adev->power.flags.ignore_parent = false;
  80. dev_err(&adapter->dev,
  81. "failed to add I2C device %s from ACPI\n",
  82. dev_name(&adev->dev));
  83. }
  84. return AE_OK;
  85. }
  86. /**
  87. * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
  88. * @adap: pointer to adapter
  89. *
  90. * Enumerate all I2C slave devices behind this adapter by walking the ACPI
  91. * namespace. When a device is found it will be added to the Linux device
  92. * model and bound to the corresponding ACPI handle.
  93. */
  94. void acpi_i2c_register_devices(struct i2c_adapter *adap)
  95. {
  96. acpi_handle handle;
  97. acpi_status status;
  98. if (!adap->dev.parent)
  99. return;
  100. handle = ACPI_HANDLE(adap->dev.parent);
  101. if (!handle)
  102. return;
  103. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  104. acpi_i2c_add_device, NULL,
  105. adap, NULL);
  106. if (ACPI_FAILURE(status))
  107. dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
  108. }
  109. #ifdef CONFIG_ACPI_I2C_OPREGION
  110. static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
  111. u8 cmd, u8 *data, u8 data_len)
  112. {
  113. struct i2c_msg msgs[2];
  114. int ret;
  115. u8 *buffer;
  116. buffer = kzalloc(data_len, GFP_KERNEL);
  117. if (!buffer)
  118. return AE_NO_MEMORY;
  119. msgs[0].addr = client->addr;
  120. msgs[0].flags = client->flags;
  121. msgs[0].len = 1;
  122. msgs[0].buf = &cmd;
  123. msgs[1].addr = client->addr;
  124. msgs[1].flags = client->flags | I2C_M_RD;
  125. msgs[1].len = data_len;
  126. msgs[1].buf = buffer;
  127. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  128. if (ret < 0)
  129. dev_err(&client->adapter->dev, "i2c read failed\n");
  130. else
  131. memcpy(data, buffer, data_len);
  132. kfree(buffer);
  133. return ret;
  134. }
  135. static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
  136. u8 cmd, u8 *data, u8 data_len)
  137. {
  138. struct i2c_msg msgs[1];
  139. u8 *buffer;
  140. int ret = AE_OK;
  141. buffer = kzalloc(data_len + 1, GFP_KERNEL);
  142. if (!buffer)
  143. return AE_NO_MEMORY;
  144. buffer[0] = cmd;
  145. memcpy(buffer + 1, data, data_len);
  146. msgs[0].addr = client->addr;
  147. msgs[0].flags = client->flags;
  148. msgs[0].len = data_len + 1;
  149. msgs[0].buf = buffer;
  150. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  151. if (ret < 0)
  152. dev_err(&client->adapter->dev, "i2c write failed\n");
  153. kfree(buffer);
  154. return ret;
  155. }
  156. static acpi_status
  157. acpi_i2c_space_handler(u32 function, acpi_physical_address command,
  158. u32 bits, u64 *value64,
  159. void *handler_context, void *region_context)
  160. {
  161. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  162. struct acpi_i2c_handler_data *data = handler_context;
  163. struct acpi_connection_info *info = &data->info;
  164. struct acpi_resource_i2c_serialbus *sb;
  165. struct i2c_adapter *adapter = data->adapter;
  166. struct i2c_client client;
  167. struct acpi_resource *ares;
  168. u32 accessor_type = function >> 16;
  169. u8 action = function & ACPI_IO_MASK;
  170. acpi_status ret = AE_OK;
  171. int status;
  172. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  173. if (ACPI_FAILURE(ret))
  174. return ret;
  175. if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  176. ret = AE_BAD_PARAMETER;
  177. goto err;
  178. }
  179. sb = &ares->data.i2c_serial_bus;
  180. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  181. ret = AE_BAD_PARAMETER;
  182. goto err;
  183. }
  184. memset(&client, 0, sizeof(client));
  185. client.adapter = adapter;
  186. client.addr = sb->slave_address;
  187. client.flags = 0;
  188. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  189. client.flags |= I2C_CLIENT_TEN;
  190. switch (accessor_type) {
  191. case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
  192. if (action == ACPI_READ) {
  193. status = i2c_smbus_read_byte(&client);
  194. if (status >= 0) {
  195. gsb->bdata = status;
  196. status = 0;
  197. }
  198. } else {
  199. status = i2c_smbus_write_byte(&client, gsb->bdata);
  200. }
  201. break;
  202. case ACPI_GSB_ACCESS_ATTRIB_BYTE:
  203. if (action == ACPI_READ) {
  204. status = i2c_smbus_read_byte_data(&client, command);
  205. if (status >= 0) {
  206. gsb->bdata = status;
  207. status = 0;
  208. }
  209. } else {
  210. status = i2c_smbus_write_byte_data(&client, command,
  211. gsb->bdata);
  212. }
  213. break;
  214. case ACPI_GSB_ACCESS_ATTRIB_WORD:
  215. if (action == ACPI_READ) {
  216. status = i2c_smbus_read_word_data(&client, command);
  217. if (status >= 0) {
  218. gsb->wdata = status;
  219. status = 0;
  220. }
  221. } else {
  222. status = i2c_smbus_write_word_data(&client, command,
  223. gsb->wdata);
  224. }
  225. break;
  226. case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
  227. if (action == ACPI_READ) {
  228. status = i2c_smbus_read_block_data(&client, command,
  229. gsb->data);
  230. if (status >= 0) {
  231. gsb->len = status;
  232. status = 0;
  233. }
  234. } else {
  235. status = i2c_smbus_write_block_data(&client, command,
  236. gsb->len, gsb->data);
  237. }
  238. break;
  239. case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
  240. if (action == ACPI_READ) {
  241. status = acpi_gsb_i2c_read_bytes(&client, command,
  242. gsb->data, info->access_length);
  243. if (status > 0)
  244. status = 0;
  245. } else {
  246. status = acpi_gsb_i2c_write_bytes(&client, command,
  247. gsb->data, info->access_length);
  248. }
  249. break;
  250. default:
  251. pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
  252. ret = AE_BAD_PARAMETER;
  253. goto err;
  254. }
  255. gsb->status = status;
  256. err:
  257. ACPI_FREE(ares);
  258. return ret;
  259. }
  260. int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
  261. {
  262. acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
  263. struct acpi_i2c_handler_data *data;
  264. acpi_status status;
  265. if (!handle)
  266. return -ENODEV;
  267. data = kzalloc(sizeof(struct acpi_i2c_handler_data),
  268. GFP_KERNEL);
  269. if (!data)
  270. return -ENOMEM;
  271. data->adapter = adapter;
  272. status = acpi_bus_attach_private_data(handle, (void *)data);
  273. if (ACPI_FAILURE(status)) {
  274. kfree(data);
  275. return -ENOMEM;
  276. }
  277. status = acpi_install_address_space_handler(handle,
  278. ACPI_ADR_SPACE_GSBUS,
  279. &acpi_i2c_space_handler,
  280. NULL,
  281. data);
  282. if (ACPI_FAILURE(status)) {
  283. dev_err(&adapter->dev, "Error installing i2c space handler\n");
  284. acpi_bus_detach_private_data(handle);
  285. kfree(data);
  286. return -ENOMEM;
  287. }
  288. return 0;
  289. }
  290. void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
  291. {
  292. acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
  293. struct acpi_i2c_handler_data *data;
  294. acpi_status status;
  295. if (!handle)
  296. return;
  297. acpi_remove_address_space_handler(handle,
  298. ACPI_ADR_SPACE_GSBUS,
  299. &acpi_i2c_space_handler);
  300. status = acpi_bus_get_private_data(handle, (void **)&data);
  301. if (ACPI_SUCCESS(status))
  302. kfree(data);
  303. acpi_bus_detach_private_data(handle);
  304. }
  305. #endif