inv_mpu_acpi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * inv_mpu_acpi: ACPI processing for creating client devices
  3. * Copyright (c) 2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifdef CONFIG_ACPI
  15. #include <linux/kernel.h>
  16. #include <linux/i2c.h>
  17. #include <linux/dmi.h>
  18. #include <linux/acpi.h>
  19. #include "inv_mpu_iio.h"
  20. enum inv_mpu_product_name {
  21. INV_MPU_NOT_MATCHED,
  22. INV_MPU_ASUS_T100TA,
  23. };
  24. static enum inv_mpu_product_name matched_product_name;
  25. static int __init asus_t100_matched(const struct dmi_system_id *d)
  26. {
  27. matched_product_name = INV_MPU_ASUS_T100TA;
  28. return 0;
  29. }
  30. static const struct dmi_system_id inv_mpu_dev_list[] = {
  31. {
  32. .callback = asus_t100_matched,
  33. .ident = "Asus Transformer Book T100",
  34. .matches = {
  35. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC"),
  36. DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
  37. DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
  38. },
  39. },
  40. /* Add more matching tables here..*/
  41. {}
  42. };
  43. static int asus_acpi_get_sensor_info(struct acpi_device *adev,
  44. struct i2c_client *client,
  45. struct i2c_board_info *info)
  46. {
  47. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  48. int i;
  49. acpi_status status;
  50. union acpi_object *cpm;
  51. int ret;
  52. status = acpi_evaluate_object(adev->handle, "CNF0", NULL, &buffer);
  53. if (ACPI_FAILURE(status))
  54. return -ENODEV;
  55. cpm = buffer.pointer;
  56. for (i = 0; i < cpm->package.count; ++i) {
  57. union acpi_object *elem;
  58. int j;
  59. elem = &cpm->package.elements[i];
  60. for (j = 0; j < elem->package.count; ++j) {
  61. union acpi_object *sub_elem;
  62. sub_elem = &elem->package.elements[j];
  63. if (sub_elem->type == ACPI_TYPE_STRING)
  64. strlcpy(info->type, sub_elem->string.pointer,
  65. sizeof(info->type));
  66. else if (sub_elem->type == ACPI_TYPE_INTEGER) {
  67. if (sub_elem->integer.value != client->addr) {
  68. info->addr = sub_elem->integer.value;
  69. break; /* Not a MPU6500 primary */
  70. }
  71. }
  72. }
  73. }
  74. ret = cpm->package.count;
  75. kfree(buffer.pointer);
  76. return ret;
  77. }
  78. static int acpi_i2c_check_resource(struct acpi_resource *ares, void *data)
  79. {
  80. u32 *addr = data;
  81. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  82. struct acpi_resource_i2c_serialbus *sb;
  83. sb = &ares->data.i2c_serial_bus;
  84. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  85. if (*addr)
  86. *addr |= (sb->slave_address << 16);
  87. else
  88. *addr = sb->slave_address;
  89. }
  90. }
  91. /* Tell the ACPI core that we already copied this address */
  92. return 1;
  93. }
  94. static int inv_mpu_process_acpi_config(struct i2c_client *client,
  95. unsigned short *primary_addr,
  96. unsigned short *secondary_addr)
  97. {
  98. const struct acpi_device_id *id;
  99. struct acpi_device *adev;
  100. u32 i2c_addr = 0;
  101. LIST_HEAD(resources);
  102. int ret;
  103. id = acpi_match_device(client->dev.driver->acpi_match_table,
  104. &client->dev);
  105. if (!id)
  106. return -ENODEV;
  107. adev = ACPI_COMPANION(&client->dev);
  108. if (!adev)
  109. return -ENODEV;
  110. ret = acpi_dev_get_resources(adev, &resources,
  111. acpi_i2c_check_resource, &i2c_addr);
  112. if (ret < 0)
  113. return ret;
  114. acpi_dev_free_resource_list(&resources);
  115. *primary_addr = i2c_addr & 0x0000ffff;
  116. *secondary_addr = (i2c_addr & 0xffff0000) >> 16;
  117. return 0;
  118. }
  119. int inv_mpu_acpi_create_mux_client(struct i2c_client *client)
  120. {
  121. struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
  122. st->mux_client = NULL;
  123. if (ACPI_HANDLE(&client->dev)) {
  124. struct i2c_board_info info;
  125. struct acpi_device *adev;
  126. int ret = -1;
  127. adev = ACPI_COMPANION(&client->dev);
  128. memset(&info, 0, sizeof(info));
  129. dmi_check_system(inv_mpu_dev_list);
  130. switch (matched_product_name) {
  131. case INV_MPU_ASUS_T100TA:
  132. ret = asus_acpi_get_sensor_info(adev, client,
  133. &info);
  134. break;
  135. /* Add more matched product processing here */
  136. default:
  137. break;
  138. }
  139. if (ret < 0) {
  140. /* No matching DMI, so create device on INV6XX type */
  141. unsigned short primary, secondary;
  142. ret = inv_mpu_process_acpi_config(client, &primary,
  143. &secondary);
  144. if (!ret && secondary) {
  145. char *name;
  146. info.addr = secondary;
  147. strlcpy(info.type, dev_name(&adev->dev),
  148. sizeof(info.type));
  149. name = strchr(info.type, ':');
  150. if (name)
  151. *name = '\0';
  152. strlcat(info.type, "-client",
  153. sizeof(info.type));
  154. } else
  155. return 0; /* no secondary addr, which is OK */
  156. }
  157. st->mux_client = i2c_new_device(st->muxc->adapter[0], &info);
  158. if (!st->mux_client)
  159. return -ENODEV;
  160. }
  161. return 0;
  162. }
  163. void inv_mpu_acpi_delete_mux_client(struct i2c_client *client)
  164. {
  165. struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(&client->dev));
  166. if (st->mux_client)
  167. i2c_unregister_device(st->mux_client);
  168. }
  169. #else
  170. #include "inv_mpu_iio.h"
  171. int inv_mpu_acpi_create_mux_client(struct i2c_client *client)
  172. {
  173. return 0;
  174. }
  175. void inv_mpu_acpi_delete_mux_client(struct i2c_client *client)
  176. {
  177. }
  178. #endif