1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * SDW Intel Init Routines
7 * Initializes and creates SDW devices based on ACPI and Hardware values
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/soundwire/sdw_intel.h>
17 #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */
18 #define SDW_MAX_LINKS 4
19 #define SDW_SHIM_LCAP 0x0
20 #define SDW_SHIM_BASE 0x2C000
21 #define SDW_ALH_BASE 0x2C800
22 #define SDW_LINK_BASE 0x30000
23 #define SDW_LINK_SIZE 0x10000
26 module_param_named(sdw_link_mask, link_mask, int, 0444);
27 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
29 struct sdw_link_data {
30 struct sdw_intel_link_res res;
31 struct platform_device *pdev;
34 struct sdw_intel_ctx {
36 struct sdw_link_data *links;
39 static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
41 struct sdw_link_data *link = ctx->links;
47 for (i = 0; i < ctx->count; i++) {
49 platform_device_unregister(link->pdev);
59 static struct sdw_intel_ctx
60 *sdw_intel_add_controller(struct sdw_intel_res *res)
62 struct platform_device_info pdevinfo;
63 struct platform_device *pdev;
64 struct sdw_link_data *link;
65 struct sdw_intel_ctx *ctx;
66 struct acpi_device *adev;
71 if (acpi_bus_get_device(res->handle, &adev))
74 /* Found controller, find links supported */
76 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
77 "mipi-sdw-master-count", &count, 1);
79 /* Don't fail on error, continue and use hw value */
82 "Failed to read mipi-sdw-master-count: %d\n", ret);
83 count = SDW_MAX_LINKS;
86 /* Check SNDWLCAP.LCOUNT */
87 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
88 caps &= GENMASK(2, 0);
90 /* Check HW supported vs property value and use min of two */
91 count = min_t(u8, caps, count);
93 /* Check count is within bounds */
94 if (count > SDW_MAX_LINKS) {
95 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
96 count, SDW_MAX_LINKS);
99 dev_warn(&adev->dev, "No SoundWire links detected\n");
103 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
105 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
110 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
116 /* Create SDW Master devices */
117 for (i = 0; i < count; i++) {
118 if (link_mask && !(link_mask & BIT(i))) {
120 "Link %d masked, will not be enabled\n", i);
125 link->res.irq = res->irq;
126 link->res.registers = res->mmio_base + SDW_LINK_BASE
127 + (SDW_LINK_SIZE * i);
128 link->res.shim = res->mmio_base + SDW_SHIM_BASE;
129 link->res.alh = res->mmio_base + SDW_ALH_BASE;
131 link->res.ops = res->ops;
132 link->res.arg = res->arg;
134 memset(&pdevinfo, 0, sizeof(pdevinfo));
136 pdevinfo.parent = res->parent;
137 pdevinfo.name = "int-sdw";
139 pdevinfo.fwnode = acpi_fwnode_handle(adev);
140 pdevinfo.data = &link->res;
141 pdevinfo.size_data = sizeof(link->res);
143 pdev = platform_device_register_full(&pdevinfo);
146 "platform device creation failed: %ld\n",
158 sdw_intel_cleanup_pdev(ctx);
164 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
165 void *cdata, void **return_value)
167 struct sdw_intel_res *res = cdata;
168 struct acpi_device *adev;
172 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
173 if (ACPI_FAILURE(status))
174 return AE_OK; /* keep going */
176 if (acpi_bus_get_device(handle, &adev)) {
177 pr_err("%s: Couldn't find ACPI handle\n", __func__);
181 res->handle = handle;
184 * On some Intel platforms, multiple children of the HDAS
185 * device can be found, but only one of them is the SoundWire
186 * controller. The SNDW device is always exposed with
187 * Name(_ADR, 0x40000000), with bits 31..28 representing the
188 * SoundWire link so filter accordingly
190 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
191 return AE_OK; /* keep going */
193 /* device found, stop namespace walk */
194 return AE_CTRL_TERMINATE;
198 * sdw_intel_init() - SoundWire Intel init routine
199 * @parent_handle: ACPI parent handle
200 * @res: resource data
202 * This scans the namespace and creates SoundWire link controller devices
203 * based on the info queried.
205 void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
209 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
213 if (ACPI_FAILURE(status))
216 return sdw_intel_add_controller(res);
218 EXPORT_SYMBOL(sdw_intel_init);
221 * sdw_intel_exit() - SoundWire Intel exit
222 * @arg: callback context
224 * Delete the controller instances created and cleanup
226 void sdw_intel_exit(void *arg)
228 struct sdw_intel_ctx *ctx = arg;
230 sdw_intel_cleanup_pdev(ctx);
233 EXPORT_SYMBOL(sdw_intel_exit);
235 MODULE_LICENSE("Dual BSD/GPL");
236 MODULE_DESCRIPTION("Intel Soundwire Init Library");