perf test: Free formats for perf pmu parse test
[linux/fpc-iii.git] / drivers / soundwire / slave.c
blob611f4f5bc36abdc7b2ef2477ccbf5d7dc9af4f2f
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
4 #include <linux/acpi.h>
5 #include <linux/of.h>
6 #include <linux/soundwire/sdw.h>
7 #include <linux/soundwire/sdw_type.h>
8 #include "bus.h"
10 static void sdw_slave_release(struct device *dev)
12 struct sdw_slave *slave = dev_to_sdw_dev(dev);
14 kfree(slave);
17 static int sdw_slave_add(struct sdw_bus *bus,
18 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
20 struct sdw_slave *slave;
21 int ret;
23 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
24 if (!slave)
25 return -ENOMEM;
27 /* Initialize data structure */
28 memcpy(&slave->id, id, sizeof(*id));
29 slave->dev.parent = bus->dev;
30 slave->dev.fwnode = fwnode;
32 /* name shall be sdw:link:mfg:part:class:unique */
33 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
34 bus->link_id, id->mfg_id, id->part_id,
35 id->class_id, id->unique_id);
37 slave->dev.release = sdw_slave_release;
38 slave->dev.bus = &sdw_bus_type;
39 slave->dev.of_node = of_node_get(to_of_node(fwnode));
40 slave->bus = bus;
41 slave->status = SDW_SLAVE_UNATTACHED;
42 slave->dev_num = 0;
44 mutex_lock(&bus->bus_lock);
45 list_add_tail(&slave->node, &bus->slaves);
46 mutex_unlock(&bus->bus_lock);
48 ret = device_register(&slave->dev);
49 if (ret) {
50 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
53 * On err, don't free but drop ref as this will be freed
54 * when release method is invoked.
56 mutex_lock(&bus->bus_lock);
57 list_del(&slave->node);
58 mutex_unlock(&bus->bus_lock);
59 put_device(&slave->dev);
61 return ret;
63 sdw_slave_debugfs_init(slave);
65 return ret;
68 #if IS_ENABLED(CONFIG_ACPI)
70 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
71 * @bus: SDW bus instance
73 * Scans Master ACPI node for SDW child Slave devices and registers it.
75 int sdw_acpi_find_slaves(struct sdw_bus *bus)
77 struct acpi_device *adev, *parent;
79 parent = ACPI_COMPANION(bus->dev);
80 if (!parent) {
81 dev_err(bus->dev, "Can't find parent for acpi bind\n");
82 return -ENODEV;
85 list_for_each_entry(adev, &parent->children, node) {
86 unsigned long long addr;
87 struct sdw_slave_id id;
88 unsigned int link_id;
89 acpi_status status;
91 status = acpi_evaluate_integer(adev->handle,
92 METHOD_NAME__ADR, NULL, &addr);
94 if (ACPI_FAILURE(status)) {
95 dev_err(bus->dev, "_ADR resolution failed: %x\n",
96 status);
97 return status;
100 /* Extract link id from ADR, Bit 51 to 48 (included) */
101 link_id = (addr >> 48) & GENMASK(3, 0);
103 /* Check for link_id match */
104 if (link_id != bus->link_id)
105 continue;
107 sdw_extract_slave_id(bus, addr, &id);
110 * don't error check for sdw_slave_add as we want to continue
111 * adding Slaves
113 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
116 return 0;
119 #endif
122 * sdw_of_find_slaves() - Find Slave devices in master device tree node
123 * @bus: SDW bus instance
125 * Scans Master DT node for SDW child Slave devices and registers it.
127 int sdw_of_find_slaves(struct sdw_bus *bus)
129 struct device *dev = bus->dev;
130 struct device_node *node;
132 for_each_child_of_node(bus->dev->of_node, node) {
133 int link_id, ret, len;
134 unsigned int sdw_version;
135 const char *compat = NULL;
136 struct sdw_slave_id id;
137 const __be32 *addr;
139 compat = of_get_property(node, "compatible", NULL);
140 if (!compat)
141 continue;
143 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
144 &id.mfg_id, &id.part_id, &id.class_id);
146 if (ret != 4) {
147 dev_err(dev, "Invalid compatible string found %s\n",
148 compat);
149 continue;
152 addr = of_get_property(node, "reg", &len);
153 if (!addr || (len < 2 * sizeof(u32))) {
154 dev_err(dev, "Invalid Link and Instance ID\n");
155 continue;
158 link_id = be32_to_cpup(addr++);
159 id.unique_id = be32_to_cpup(addr);
160 id.sdw_version = sdw_version;
162 /* Check for link_id match */
163 if (link_id != bus->link_id)
164 continue;
166 sdw_slave_add(bus, &id, of_fwnode_handle(node));
169 return 0;