blob: b74c2f14496218d8d515caf65cf3333a23170e59 [file] [log] [blame]
Vinod Kould62a7d42017-12-14 11:19:44 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4/*
5 * SDW Intel Init Routines
6 *
7 * Initializes and creates SDW devices based on ACPI and Hardware values
8 */
9
10#include <linux/acpi.h>
Paul Gortmaker4abbd782019-04-13 11:12:52 -040011#include <linux/export.h>
12#include <linux/module.h>
Vinod Kould62a7d42017-12-14 11:19:44 +053013#include <linux/platform_device.h>
14#include <linux/soundwire/sdw_intel.h>
15#include "intel.h"
16
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -050017#define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */
Vinod Kould62a7d42017-12-14 11:19:44 +053018#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
24
Pierre-Louis Bossart50302fc2019-08-05 19:55:20 -050025static int link_mask;
26module_param_named(sdw_link_mask, link_mask, int, 0444);
27MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
28
Vinod Kould62a7d42017-12-14 11:19:44 +053029struct sdw_link_data {
30 struct sdw_intel_link_res res;
31 struct platform_device *pdev;
32};
33
34struct sdw_intel_ctx {
35 int count;
36 struct sdw_link_data *links;
37};
38
39static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
40{
41 struct sdw_link_data *link = ctx->links;
42 int i;
43
44 if (!link)
45 return 0;
46
47 for (i = 0; i < ctx->count; i++) {
48 if (link->pdev)
49 platform_device_unregister(link->pdev);
50 link++;
51 }
52
53 kfree(ctx->links);
54 ctx->links = NULL;
55
56 return 0;
57}
58
59static struct sdw_intel_ctx
60*sdw_intel_add_controller(struct sdw_intel_res *res)
61{
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;
67 int ret, i;
68 u8 count;
69 u32 caps;
70
71 if (acpi_bus_get_device(res->handle, &adev))
72 return NULL;
73
74 /* Found controller, find links supported */
75 count = 0;
76 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050077 "mipi-sdw-master-count", &count, 1);
Vinod Kould62a7d42017-12-14 11:19:44 +053078
79 /* Don't fail on error, continue and use hw value */
80 if (ret) {
81 dev_err(&adev->dev,
82 "Failed to read mipi-sdw-master-count: %d\n", ret);
83 count = SDW_MAX_LINKS;
84 }
85
86 /* Check SNDWLCAP.LCOUNT */
87 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
Pierre-Louis Bossart432732b2019-05-22 14:47:31 -050088 caps &= GENMASK(2, 0);
Vinod Kould62a7d42017-12-14 11:19:44 +053089
90 /* Check HW supported vs property value and use min of two */
91 count = min_t(u8, caps, count);
92
93 /* Check count is within bounds */
94 if (count > SDW_MAX_LINKS) {
95 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050096 count, SDW_MAX_LINKS);
Vinod Kould62a7d42017-12-14 11:19:44 +053097 return NULL;
Pierre-Louis Bossart432732b2019-05-22 14:47:31 -050098 } else if (!count) {
99 dev_warn(&adev->dev, "No SoundWire links detected\n");
100 return NULL;
Vinod Kould62a7d42017-12-14 11:19:44 +0530101 }
102
103 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
104
105 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
106 if (!ctx)
107 return NULL;
108
109 ctx->count = count;
110 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
111 if (!ctx->links)
112 goto link_err;
113
114 link = ctx->links;
115
116 /* Create SDW Master devices */
117 for (i = 0; i < count; i++) {
Pierre-Louis Bossart50302fc2019-08-05 19:55:20 -0500118 if (link_mask && !(link_mask & BIT(i))) {
119 dev_dbg(&adev->dev,
120 "Link %d masked, will not be enabled\n", i);
121 link++;
122 continue;
123 }
124
Vinod Kould62a7d42017-12-14 11:19:44 +0530125 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;
130
Vinod Koulc46302e2018-04-26 18:39:05 +0530131 link->res.ops = res->ops;
132 link->res.arg = res->arg;
133
Vinod Kould62a7d42017-12-14 11:19:44 +0530134 memset(&pdevinfo, 0, sizeof(pdevinfo));
135
136 pdevinfo.parent = res->parent;
137 pdevinfo.name = "int-sdw";
138 pdevinfo.id = i;
139 pdevinfo.fwnode = acpi_fwnode_handle(adev);
140 pdevinfo.data = &link->res;
141 pdevinfo.size_data = sizeof(link->res);
142
143 pdev = platform_device_register_full(&pdevinfo);
144 if (IS_ERR(pdev)) {
145 dev_err(&adev->dev,
146 "platform device creation failed: %ld\n",
147 PTR_ERR(pdev));
148 goto pdev_err;
149 }
150
151 link->pdev = pdev;
152 link++;
153 }
154
155 return ctx;
156
157pdev_err:
158 sdw_intel_cleanup_pdev(ctx);
159link_err:
160 kfree(ctx);
161 return NULL;
162}
163
164static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500165 void *cdata, void **return_value)
Vinod Kould62a7d42017-12-14 11:19:44 +0530166{
167 struct sdw_intel_res *res = cdata;
168 struct acpi_device *adev;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500169 acpi_status status;
170 u64 adr;
171
172 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
173 if (ACPI_FAILURE(status))
174 return AE_OK; /* keep going */
Vinod Kould62a7d42017-12-14 11:19:44 +0530175
176 if (acpi_bus_get_device(handle, &adev)) {
Vinod Koule1c815f2018-08-07 11:54:50 +0530177 pr_err("%s: Couldn't find ACPI handle\n", __func__);
Vinod Kould62a7d42017-12-14 11:19:44 +0530178 return AE_NOT_FOUND;
179 }
180
181 res->handle = handle;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500182
183 /*
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
189 */
190 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
191 return AE_OK; /* keep going */
192
193 /* device found, stop namespace walk */
194 return AE_CTRL_TERMINATE;
Vinod Kould62a7d42017-12-14 11:19:44 +0530195}
196
197/**
198 * sdw_intel_init() - SoundWire Intel init routine
199 * @parent_handle: ACPI parent handle
200 * @res: resource data
201 *
202 * This scans the namespace and creates SoundWire link controller devices
203 * based on the info queried.
204 */
205void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
206{
207 acpi_status status;
208
209 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500210 parent_handle, 1,
211 sdw_intel_acpi_cb,
212 NULL, res, NULL);
Vinod Kould62a7d42017-12-14 11:19:44 +0530213 if (ACPI_FAILURE(status))
214 return NULL;
215
216 return sdw_intel_add_controller(res);
217}
218EXPORT_SYMBOL(sdw_intel_init);
219
220/**
221 * sdw_intel_exit() - SoundWire Intel exit
222 * @arg: callback context
223 *
224 * Delete the controller instances created and cleanup
225 */
226void sdw_intel_exit(void *arg)
227{
228 struct sdw_intel_ctx *ctx = arg;
229
230 sdw_intel_cleanup_pdev(ctx);
231 kfree(ctx);
232}
233EXPORT_SYMBOL(sdw_intel_exit);
234
235MODULE_LICENSE("Dual BSD/GPL");
236MODULE_DESCRIPTION("Intel Soundwire Init Library");