slave.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/soundwire/sdw.h>
  5. #include <linux/soundwire/sdw_type.h>
  6. #include "bus.h"
  7. static void sdw_slave_release(struct device *dev)
  8. {
  9. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  10. kfree(slave);
  11. }
  12. static int sdw_slave_add(struct sdw_bus *bus,
  13. struct sdw_slave_id *id, struct fwnode_handle *fwnode)
  14. {
  15. struct sdw_slave *slave;
  16. int ret;
  17. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  18. if (!slave)
  19. return -ENOMEM;
  20. /* Initialize data structure */
  21. memcpy(&slave->id, id, sizeof(*id));
  22. slave->dev.parent = bus->dev;
  23. slave->dev.fwnode = fwnode;
  24. /* name shall be sdw:link:mfg:part:class:unique */
  25. dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
  26. bus->link_id, id->mfg_id, id->part_id,
  27. id->class_id, id->unique_id);
  28. slave->dev.release = sdw_slave_release;
  29. slave->dev.bus = &sdw_bus_type;
  30. slave->bus = bus;
  31. slave->status = SDW_SLAVE_UNATTACHED;
  32. slave->dev_num = 0;
  33. mutex_lock(&bus->bus_lock);
  34. list_add_tail(&slave->node, &bus->slaves);
  35. mutex_unlock(&bus->bus_lock);
  36. ret = device_register(&slave->dev);
  37. if (ret) {
  38. dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
  39. /*
  40. * On err, don't free but drop ref as this will be freed
  41. * when release method is invoked.
  42. */
  43. mutex_lock(&bus->bus_lock);
  44. list_del(&slave->node);
  45. mutex_unlock(&bus->bus_lock);
  46. put_device(&slave->dev);
  47. }
  48. return ret;
  49. }
  50. #if IS_ENABLED(CONFIG_ACPI)
  51. /*
  52. * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
  53. * @bus: SDW bus instance
  54. *
  55. * Scans Master ACPI node for SDW child Slave devices and registers it.
  56. */
  57. int sdw_acpi_find_slaves(struct sdw_bus *bus)
  58. {
  59. struct acpi_device *adev, *parent;
  60. parent = ACPI_COMPANION(bus->dev);
  61. if (!parent) {
  62. dev_err(bus->dev, "Can't find parent for acpi bind\n");
  63. return -ENODEV;
  64. }
  65. list_for_each_entry(adev, &parent->children, node) {
  66. unsigned long long addr;
  67. struct sdw_slave_id id;
  68. unsigned int link_id;
  69. acpi_status status;
  70. status = acpi_evaluate_integer(adev->handle,
  71. METHOD_NAME__ADR, NULL, &addr);
  72. if (ACPI_FAILURE(status)) {
  73. dev_err(bus->dev, "_ADR resolution failed: %x\n",
  74. status);
  75. return status;
  76. }
  77. /* Extract link id from ADR, Bit 51 to 48 (included) */
  78. link_id = (addr >> 48) & GENMASK(3, 0);
  79. /* Check for link_id match */
  80. if (link_id != bus->link_id)
  81. continue;
  82. sdw_extract_slave_id(bus, addr, &id);
  83. /*
  84. * don't error check for sdw_slave_add as we want to continue
  85. * adding Slaves
  86. */
  87. sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
  88. }
  89. return 0;
  90. }
  91. #endif