i2c-multi-instantiate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * I2C multi-instantiate driver, pseudo driver to instantiate multiple
  4. * i2c-clients from a single fwnode.
  5. *
  6. * Copyright 2018 Hans de Goede <hdegoede@redhat.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bits.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/types.h>
  16. #define IRQ_RESOURCE_TYPE GENMASK(1, 0)
  17. #define IRQ_RESOURCE_NONE 0
  18. #define IRQ_RESOURCE_GPIO 1
  19. #define IRQ_RESOURCE_APIC 2
  20. struct i2c_inst_data {
  21. const char *type;
  22. unsigned int flags;
  23. int irq_idx;
  24. };
  25. struct i2c_multi_inst_data {
  26. int num_clients;
  27. struct i2c_client *clients[];
  28. };
  29. static int i2c_multi_inst_count(struct acpi_resource *ares, void *data)
  30. {
  31. struct acpi_resource_i2c_serialbus *sb;
  32. int *count = data;
  33. if (i2c_acpi_get_i2c_resource(ares, &sb))
  34. *count = *count + 1;
  35. return 1;
  36. }
  37. static int i2c_multi_inst_count_resources(struct acpi_device *adev)
  38. {
  39. LIST_HEAD(r);
  40. int count = 0;
  41. int ret;
  42. ret = acpi_dev_get_resources(adev, &r, i2c_multi_inst_count, &count);
  43. if (ret < 0)
  44. return ret;
  45. acpi_dev_free_resource_list(&r);
  46. return count;
  47. }
  48. static int i2c_multi_inst_probe(struct platform_device *pdev)
  49. {
  50. struct i2c_multi_inst_data *multi;
  51. const struct acpi_device_id *match;
  52. const struct i2c_inst_data *inst_data;
  53. struct i2c_board_info board_info = {};
  54. struct device *dev = &pdev->dev;
  55. struct acpi_device *adev;
  56. char name[32];
  57. int i, ret;
  58. match = acpi_match_device(dev->driver->acpi_match_table, dev);
  59. if (!match) {
  60. dev_err(dev, "Error ACPI match data is missing\n");
  61. return -ENODEV;
  62. }
  63. inst_data = (const struct i2c_inst_data *)match->driver_data;
  64. adev = ACPI_COMPANION(dev);
  65. /* Count number of clients to instantiate */
  66. ret = i2c_multi_inst_count_resources(adev);
  67. if (ret < 0)
  68. return ret;
  69. multi = devm_kmalloc(dev, struct_size(multi, clients, ret), GFP_KERNEL);
  70. if (!multi)
  71. return -ENOMEM;
  72. multi->num_clients = ret;
  73. for (i = 0; i < multi->num_clients && inst_data[i].type; i++) {
  74. memset(&board_info, 0, sizeof(board_info));
  75. strlcpy(board_info.type, inst_data[i].type, I2C_NAME_SIZE);
  76. snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev),
  77. inst_data[i].type, i);
  78. board_info.dev_name = name;
  79. switch (inst_data[i].flags & IRQ_RESOURCE_TYPE) {
  80. case IRQ_RESOURCE_GPIO:
  81. ret = acpi_dev_gpio_irq_get(adev, inst_data[i].irq_idx);
  82. if (ret < 0) {
  83. dev_err(dev, "Error requesting irq at index %d: %d\n",
  84. inst_data[i].irq_idx, ret);
  85. goto error;
  86. }
  87. board_info.irq = ret;
  88. break;
  89. case IRQ_RESOURCE_APIC:
  90. ret = platform_get_irq(pdev, inst_data[i].irq_idx);
  91. if (ret < 0) {
  92. dev_dbg(dev, "Error requesting irq at index %d: %d\n",
  93. inst_data[i].irq_idx, ret);
  94. goto error;
  95. }
  96. board_info.irq = ret;
  97. break;
  98. default:
  99. board_info.irq = 0;
  100. break;
  101. }
  102. multi->clients[i] = i2c_acpi_new_device(dev, i, &board_info);
  103. if (IS_ERR(multi->clients[i])) {
  104. ret = PTR_ERR(multi->clients[i]);
  105. if (ret != -EPROBE_DEFER)
  106. dev_err(dev, "Error creating i2c-client, idx %d\n", i);
  107. goto error;
  108. }
  109. }
  110. if (i < multi->num_clients) {
  111. dev_err(dev, "Error finding driver, idx %d\n", i);
  112. ret = -ENODEV;
  113. goto error;
  114. }
  115. platform_set_drvdata(pdev, multi);
  116. return 0;
  117. error:
  118. while (--i >= 0)
  119. i2c_unregister_device(multi->clients[i]);
  120. return ret;
  121. }
  122. static int i2c_multi_inst_remove(struct platform_device *pdev)
  123. {
  124. struct i2c_multi_inst_data *multi = platform_get_drvdata(pdev);
  125. int i;
  126. for (i = 0; i < multi->num_clients; i++)
  127. i2c_unregister_device(multi->clients[i]);
  128. return 0;
  129. }
  130. static const struct i2c_inst_data bsg1160_data[] = {
  131. { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
  132. { "bmc150_magn" },
  133. { "bmg160" },
  134. {}
  135. };
  136. static const struct i2c_inst_data bsg2150_data[] = {
  137. { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
  138. { "bmc150_magn" },
  139. /* The resources describe a 3th client, but it is not really there. */
  140. { "bsg2150_dummy_dev" },
  141. {}
  142. };
  143. /*
  144. * Device with _HID INT3515 (TI PD controllers) has some unresolved interrupt
  145. * issues. The most common problem seen is interrupt flood.
  146. *
  147. * There are at least two known causes. Firstly, on some boards, the
  148. * I2CSerialBus resource index does not match the Interrupt resource, i.e. they
  149. * are not one-to-one mapped like in the array below. Secondly, on some boards
  150. * the IRQ line from the PD controller is not actually connected at all. But the
  151. * interrupt flood is also seen on some boards where those are not a problem, so
  152. * there are some other problems as well.
  153. *
  154. * Because of the issues with the interrupt, the device is disabled for now. If
  155. * you wish to debug the issues, uncomment the below, and add an entry for the
  156. * INT3515 device to the i2c_multi_instance_ids table.
  157. *
  158. * static const struct i2c_inst_data int3515_data[] = {
  159. * { "tps6598x", IRQ_RESOURCE_APIC, 0 },
  160. * { "tps6598x", IRQ_RESOURCE_APIC, 1 },
  161. * { "tps6598x", IRQ_RESOURCE_APIC, 2 },
  162. * { "tps6598x", IRQ_RESOURCE_APIC, 3 },
  163. * { }
  164. * };
  165. */
  166. /*
  167. * Note new device-ids must also be added to i2c_multi_instantiate_ids in
  168. * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
  169. */
  170. static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = {
  171. { "BSG1160", (unsigned long)bsg1160_data },
  172. { "BSG2150", (unsigned long)bsg2150_data },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids);
  176. static struct platform_driver i2c_multi_inst_driver = {
  177. .driver = {
  178. .name = "I2C multi instantiate pseudo device driver",
  179. .acpi_match_table = ACPI_PTR(i2c_multi_inst_acpi_ids),
  180. },
  181. .probe = i2c_multi_inst_probe,
  182. .remove = i2c_multi_inst_remove,
  183. };
  184. module_platform_driver(i2c_multi_inst_driver);
  185. MODULE_DESCRIPTION("I2C multi instantiate pseudo device driver");
  186. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  187. MODULE_LICENSE("GPL");