slave.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. #include <linux/acpi.h>
  4. #include <linux/of.h>
  5. #include <linux/soundwire/sdw.h>
  6. #include <linux/soundwire/sdw_type.h>
  7. #include <sound/sdca.h>
  8. #include "bus.h"
  9. #include "sysfs_local.h"
  10. static void sdw_slave_release(struct device *dev)
  11. {
  12. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  13. of_node_put(slave->dev.of_node);
  14. mutex_destroy(&slave->sdw_dev_lock);
  15. kfree(slave);
  16. }
  17. const struct device_type sdw_slave_type = {
  18. .name = "sdw_slave",
  19. .release = sdw_slave_release,
  20. .uevent = sdw_slave_uevent,
  21. };
  22. EXPORT_SYMBOL_GPL(sdw_slave_type);
  23. int sdw_slave_add(struct sdw_bus *bus,
  24. struct sdw_slave_id *id, struct fwnode_handle *fwnode)
  25. {
  26. struct sdw_slave *slave;
  27. int ret;
  28. int i;
  29. slave = kzalloc_obj(*slave);
  30. if (!slave)
  31. return -ENOMEM;
  32. /* Initialize data structure */
  33. memcpy(&slave->id, id, sizeof(*id));
  34. slave->dev.parent = bus->dev;
  35. slave->dev.fwnode = fwnode;
  36. if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
  37. /* name shall be sdw:ctrl:link:mfg:part:class */
  38. dev_set_name(&slave->dev, "sdw:%01x:%01x:%04x:%04x:%02x",
  39. bus->controller_id, bus->link_id, id->mfg_id, id->part_id,
  40. id->class_id);
  41. } else {
  42. /* name shall be sdw:ctrl:link:mfg:part:class:unique */
  43. dev_set_name(&slave->dev, "sdw:%01x:%01x:%04x:%04x:%02x:%01x",
  44. bus->controller_id, bus->link_id, id->mfg_id, id->part_id,
  45. id->class_id, id->unique_id);
  46. }
  47. slave->dev.bus = &sdw_bus_type;
  48. slave->dev.of_node = of_node_get(to_of_node(fwnode));
  49. slave->dev.type = &sdw_slave_type;
  50. slave->dev.groups = sdw_slave_status_attr_groups;
  51. slave->bus = bus;
  52. slave->status = SDW_SLAVE_UNATTACHED;
  53. init_completion(&slave->enumeration_complete);
  54. init_completion(&slave->initialization_complete);
  55. slave->dev_num = 0;
  56. slave->probed = false;
  57. slave->first_interrupt_done = false;
  58. mutex_init(&slave->sdw_dev_lock);
  59. for (i = 0; i < SDW_MAX_PORTS; i++)
  60. init_completion(&slave->port_ready[i]);
  61. mutex_lock(&bus->bus_lock);
  62. list_add_tail(&slave->node, &bus->slaves);
  63. mutex_unlock(&bus->bus_lock);
  64. /*
  65. * The Soundwire driver probe may optionally register SDCA
  66. * sub-devices, one per Function. This means the information
  67. * on the SDCA revision and the number/type of Functions need
  68. * to be extracted from platform firmware before the SoundWire
  69. * driver probe, and as a consequence before the SoundWire
  70. * device_register() below.
  71. */
  72. sdca_lookup_interface_revision(slave);
  73. sdca_lookup_functions(slave);
  74. ret = device_register(&slave->dev);
  75. if (ret) {
  76. dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
  77. /*
  78. * On err, don't free but drop ref as this will be freed
  79. * when release method is invoked.
  80. */
  81. mutex_lock(&bus->bus_lock);
  82. list_del(&slave->node);
  83. mutex_unlock(&bus->bus_lock);
  84. put_device(&slave->dev);
  85. return ret;
  86. }
  87. sdw_slave_debugfs_init(slave);
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(sdw_slave_add);
  91. #if IS_ENABLED(CONFIG_ACPI)
  92. static bool find_slave(struct sdw_bus *bus,
  93. struct acpi_device *adev,
  94. struct sdw_slave_id *id)
  95. {
  96. unsigned int link_id;
  97. u64 addr;
  98. int ret;
  99. ret = acpi_get_local_u64_address(adev->handle, &addr);
  100. if (ret < 0)
  101. return false;
  102. if (bus->ops->override_adr)
  103. addr = bus->ops->override_adr(bus, addr);
  104. if (!addr)
  105. return false;
  106. /* Extract link id from ADR, Bit 51 to 48 (included) */
  107. link_id = SDW_DISCO_LINK_ID(addr);
  108. /* Check for link_id match */
  109. if (link_id != bus->link_id)
  110. return false;
  111. sdw_extract_slave_id(bus, addr, id);
  112. return true;
  113. }
  114. struct sdw_acpi_child_walk_data {
  115. struct sdw_bus *bus;
  116. struct acpi_device *adev;
  117. struct sdw_slave_id id;
  118. bool ignore_unique_id;
  119. };
  120. static int sdw_acpi_check_duplicate(struct acpi_device *adev, void *data)
  121. {
  122. struct sdw_acpi_child_walk_data *cwd = data;
  123. struct sdw_bus *bus = cwd->bus;
  124. struct sdw_slave_id id;
  125. if (adev == cwd->adev)
  126. return 0;
  127. if (!find_slave(bus, adev, &id))
  128. return 0;
  129. if (cwd->id.sdw_version != id.sdw_version || cwd->id.mfg_id != id.mfg_id ||
  130. cwd->id.part_id != id.part_id || cwd->id.class_id != id.class_id)
  131. return 0;
  132. if (cwd->id.unique_id != id.unique_id) {
  133. dev_dbg(bus->dev,
  134. "Valid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
  135. cwd->id.unique_id, id.unique_id, cwd->id.mfg_id,
  136. cwd->id.part_id);
  137. cwd->ignore_unique_id = false;
  138. return 0;
  139. }
  140. dev_err(bus->dev,
  141. "Invalid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
  142. cwd->id.unique_id, id.unique_id, cwd->id.mfg_id, cwd->id.part_id);
  143. return -ENODEV;
  144. }
  145. static int sdw_acpi_find_one(struct acpi_device *adev, void *data)
  146. {
  147. struct sdw_bus *bus = data;
  148. struct sdw_acpi_child_walk_data cwd = {
  149. .bus = bus,
  150. .adev = adev,
  151. .ignore_unique_id = true,
  152. };
  153. int ret;
  154. if (!find_slave(bus, adev, &cwd.id))
  155. return 0;
  156. /* Brute-force O(N^2) search for duplicates. */
  157. ret = acpi_dev_for_each_child(ACPI_COMPANION(bus->dev),
  158. sdw_acpi_check_duplicate, &cwd);
  159. if (ret)
  160. return ret;
  161. if (cwd.ignore_unique_id)
  162. cwd.id.unique_id = SDW_IGNORED_UNIQUE_ID;
  163. /* Ignore errors and continue. */
  164. sdw_slave_add(bus, &cwd.id, acpi_fwnode_handle(adev));
  165. return 0;
  166. }
  167. /*
  168. * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
  169. * @bus: SDW bus instance
  170. *
  171. * Scans Master ACPI node for SDW child Slave devices and registers it.
  172. */
  173. int sdw_acpi_find_slaves(struct sdw_bus *bus)
  174. {
  175. struct acpi_device *parent;
  176. parent = ACPI_COMPANION(bus->dev);
  177. if (!parent) {
  178. dev_err(bus->dev, "Can't find parent for acpi bind\n");
  179. return -ENODEV;
  180. }
  181. return acpi_dev_for_each_child(parent, sdw_acpi_find_one, bus);
  182. }
  183. #endif
  184. /*
  185. * sdw_of_find_slaves() - Find Slave devices in master device tree node
  186. * @bus: SDW bus instance
  187. *
  188. * Scans Master DT node for SDW child Slave devices and registers it.
  189. */
  190. int sdw_of_find_slaves(struct sdw_bus *bus)
  191. {
  192. struct device *dev = bus->dev;
  193. struct device_node *node;
  194. for_each_child_of_node(bus->dev->of_node, node) {
  195. int link_id, ret, len;
  196. unsigned int sdw_version;
  197. const char *compat = NULL;
  198. struct sdw_slave_id id;
  199. const __be32 *addr;
  200. compat = of_get_property(node, "compatible", NULL);
  201. if (!compat)
  202. continue;
  203. ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
  204. &id.mfg_id, &id.part_id, &id.class_id);
  205. if (ret != 4) {
  206. dev_err(dev, "Invalid compatible string found %s\n",
  207. compat);
  208. continue;
  209. }
  210. addr = of_get_property(node, "reg", &len);
  211. if (!addr || (len < 2 * sizeof(u32))) {
  212. dev_err(dev, "Invalid Link and Instance ID\n");
  213. continue;
  214. }
  215. link_id = be32_to_cpup(addr++);
  216. id.unique_id = be32_to_cpup(addr);
  217. id.sdw_version = sdw_version;
  218. /* Check for link_id match */
  219. if (link_id != bus->link_id)
  220. continue;
  221. sdw_slave_add(bus, &id, of_fwnode_handle(node));
  222. }
  223. return 0;
  224. }
  225. struct device *of_sdw_find_device_by_node(struct device_node *np)
  226. {
  227. return bus_find_device_by_of_node(&sdw_bus_type, np);
  228. }
  229. EXPORT_SYMBOL_GPL(of_sdw_find_device_by_node);
  230. MODULE_IMPORT_NS("SND_SOC_SDCA");