Loading...
1// 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>
11#include <linux/export.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/auxiliary_bus.h>
16#include <linux/pm_runtime.h>
17#include <linux/soundwire/sdw_intel.h>
18#include "cadence_master.h"
19#include "bus.h"
20#include "intel.h"
21#include "intel_auxdevice.h"
22
23static void intel_link_dev_release(struct device *dev)
24{
25 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
26 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
27
28 kfree(ldev);
29}
30
31/* alloc, init and add link devices */
32static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
33 struct sdw_intel_ctx *ctx,
34 struct fwnode_handle *fwnode,
35 const char *name,
36 int link_id)
37{
38 struct sdw_intel_link_dev *ldev;
39 struct sdw_intel_link_res *link;
40 struct auxiliary_device *auxdev;
41 int ret;
42
43 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
44 if (!ldev)
45 return ERR_PTR(-ENOMEM);
46
47 auxdev = &ldev->auxdev;
48 auxdev->name = name;
49 auxdev->dev.parent = res->parent;
50 auxdev->dev.fwnode = fwnode;
51 auxdev->dev.release = intel_link_dev_release;
52
53 /* we don't use an IDA since we already have a link ID */
54 auxdev->id = link_id;
55
56 /*
57 * keep a handle on the allocated memory, to be used in all other functions.
58 * Since the same pattern is used to skip links that are not enabled, there is
59 * no need to check if ctx->ldev[i] is NULL later on.
60 */
61 ctx->ldev[link_id] = ldev;
62
63 /* Add link information used in the driver probe */
64 link = &ldev->link_res;
65 link->hw_ops = res->hw_ops;
66 link->mmio_base = res->mmio_base;
67 if (!res->ext) {
68 link->registers = res->mmio_base + SDW_LINK_BASE
69 + (SDW_LINK_SIZE * link_id);
70 link->ip_offset = 0;
71 link->shim = res->mmio_base + res->shim_base;
72 link->alh = res->mmio_base + res->alh_base;
73 link->shim_lock = &ctx->shim_lock;
74 } else {
75 link->registers = res->mmio_base + SDW_IP_BASE(link_id);
76 link->ip_offset = SDW_CADENCE_MCP_IP_OFFSET;
77 link->shim = res->mmio_base + SDW_SHIM2_GENERIC_BASE(link_id);
78 link->shim_vs = res->mmio_base + SDW_SHIM2_VS_BASE(link_id);
79 link->shim_lock = res->eml_lock;
80 }
81
82 link->ops = res->ops;
83 link->dev = res->dev;
84
85 link->clock_stop_quirks = res->clock_stop_quirks;
86 link->shim_mask = &ctx->shim_mask;
87 link->link_mask = ctx->link_mask;
88
89 link->hbus = res->hbus;
90
91 /* now follow the two-step init/add sequence */
92 ret = auxiliary_device_init(auxdev);
93 if (ret < 0) {
94 dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
95 name, link_id);
96 kfree(ldev);
97 return ERR_PTR(ret);
98 }
99
100 ret = auxiliary_device_add(&ldev->auxdev);
101 if (ret < 0) {
102 dev_err(res->parent, "failed to add link dev %s link_id %d\n",
103 ldev->auxdev.name, link_id);
104 /* ldev will be freed with the put_device() and .release sequence */
105 auxiliary_device_uninit(&ldev->auxdev);
106 return ERR_PTR(ret);
107 }
108
109 return ldev;
110}
111
112static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
113{
114 auxiliary_device_delete(&ldev->auxdev);
115 auxiliary_device_uninit(&ldev->auxdev);
116}
117
118static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
119{
120 struct sdw_intel_link_dev *ldev;
121 u32 link_mask;
122 int i;
123
124 link_mask = ctx->link_mask;
125
126 for (i = 0; i < ctx->count; i++) {
127 if (!(link_mask & BIT(i)))
128 continue;
129
130 ldev = ctx->ldev[i];
131
132 pm_runtime_disable(&ldev->auxdev.dev);
133 if (!ldev->link_res.clock_stop_quirks)
134 pm_runtime_put_noidle(ldev->link_res.dev);
135
136 intel_link_dev_unregister(ldev);
137 }
138
139 return 0;
140}
141
142irqreturn_t sdw_intel_thread(int irq, void *dev_id)
143{
144 struct sdw_intel_ctx *ctx = dev_id;
145 struct sdw_intel_link_res *link;
146
147 list_for_each_entry(link, &ctx->link_list, list)
148 sdw_cdns_irq(irq, link->cdns);
149
150 return IRQ_HANDLED;
151}
152EXPORT_SYMBOL_NS(sdw_intel_thread, "SOUNDWIRE_INTEL_INIT");
153
154static struct sdw_intel_ctx
155*sdw_intel_probe_controller(struct sdw_intel_res *res)
156{
157 struct sdw_intel_link_res *link;
158 struct sdw_intel_link_dev *ldev;
159 struct sdw_intel_ctx *ctx;
160 struct acpi_device *adev;
161 struct sdw_slave *slave;
162 struct list_head *node;
163 struct sdw_bus *bus;
164 u32 link_mask;
165 int num_slaves = 0;
166 int count;
167 int i;
168
169 if (!res)
170 return NULL;
171
172 adev = acpi_fetch_acpi_dev(res->handle);
173 if (!adev)
174 return NULL;
175
176 if (!res->count)
177 return NULL;
178
179 count = res->count;
180 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
181
182 /*
183 * we need to alloc/free memory manually and can't use devm:
184 * this routine may be called from a workqueue, and not from
185 * the parent .probe.
186 * If devm_ was used, the memory might never be freed on errors.
187 */
188 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
189 if (!ctx)
190 return NULL;
191
192 ctx->count = count;
193
194 /*
195 * allocate the array of pointers. The link-specific data is allocated
196 * as part of the first loop below and released with the auxiliary_device_uninit().
197 * If some links are disabled, the link pointer will remain NULL. Given that the
198 * number of links is small, this is simpler than using a list to keep track of links.
199 */
200 ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), GFP_KERNEL);
201 if (!ctx->ldev) {
202 kfree(ctx);
203 return NULL;
204 }
205
206 ctx->mmio_base = res->mmio_base;
207 ctx->shim_base = res->shim_base;
208 ctx->alh_base = res->alh_base;
209 ctx->link_mask = res->link_mask;
210 ctx->handle = res->handle;
211 mutex_init(&ctx->shim_lock);
212
213 link_mask = ctx->link_mask;
214
215 INIT_LIST_HEAD(&ctx->link_list);
216
217 for (i = 0; i < count; i++) {
218 if (!(link_mask & BIT(i)))
219 continue;
220
221 /*
222 * init and add a device for each link
223 *
224 * The name of the device will be soundwire_intel.link.[i],
225 * with the "soundwire_intel" module prefix automatically added
226 * by the auxiliary bus core.
227 */
228 ldev = intel_link_dev_register(res,
229 ctx,
230 acpi_fwnode_handle(adev),
231 "link",
232 i);
233 if (IS_ERR(ldev))
234 goto err;
235
236 link = &ldev->link_res;
237 link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
238
239 if (!link->cdns) {
240 dev_err(&adev->dev, "failed to get link->cdns\n");
241 /*
242 * 1 will be subtracted from i in the err label, but we need to call
243 * intel_link_dev_unregister for this ldev, so plus 1 now
244 */
245 i++;
246 goto err;
247 }
248 list_add_tail(&link->list, &ctx->link_list);
249 bus = &link->cdns->bus;
250 /* Calculate number of slaves */
251 list_for_each(node, &bus->slaves)
252 num_slaves++;
253 }
254
255 ctx->peripherals = kmalloc(struct_size(ctx->peripherals, array, num_slaves),
256 GFP_KERNEL);
257 if (!ctx->peripherals)
258 goto err;
259 ctx->peripherals->num_peripherals = num_slaves;
260 i = 0;
261 list_for_each_entry(link, &ctx->link_list, list) {
262 bus = &link->cdns->bus;
263 list_for_each_entry(slave, &bus->slaves, node) {
264 ctx->peripherals->array[i] = slave;
265 i++;
266 }
267 }
268
269 return ctx;
270
271err:
272 while (i--) {
273 if (!(link_mask & BIT(i)))
274 continue;
275 ldev = ctx->ldev[i];
276 intel_link_dev_unregister(ldev);
277 }
278 kfree(ctx->ldev);
279 kfree(ctx);
280 return NULL;
281}
282
283static int
284sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
285{
286 struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
287 struct sdw_intel_link_dev *ldev;
288 u32 link_mask;
289 int i;
290
291 if (!adev)
292 return -EINVAL;
293
294 if (!ctx->ldev)
295 return -EINVAL;
296
297 link_mask = ctx->link_mask;
298
299 /* Startup SDW Master devices */
300 for (i = 0; i < ctx->count; i++) {
301 if (!(link_mask & BIT(i)))
302 continue;
303
304 ldev = ctx->ldev[i];
305
306 intel_link_startup(&ldev->auxdev);
307
308 if (!ldev->link_res.clock_stop_quirks) {
309 /*
310 * we need to prevent the parent PCI device
311 * from entering pm_runtime suspend, so that
312 * power rails to the SoundWire IP are not
313 * turned off.
314 */
315 pm_runtime_get_noresume(ldev->link_res.dev);
316 }
317 }
318
319 return 0;
320}
321
322/**
323 * sdw_intel_probe() - SoundWire Intel probe routine
324 * @res: resource data
325 *
326 * This registers an auxiliary device for each Master handled by the controller,
327 * and SoundWire Master and Slave devices will be created by the auxiliary
328 * device probe. All the information necessary is stored in the context, and
329 * the res argument pointer can be freed after this step.
330 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
331 */
332struct sdw_intel_ctx
333*sdw_intel_probe(struct sdw_intel_res *res)
334{
335 return sdw_intel_probe_controller(res);
336}
337EXPORT_SYMBOL_NS(sdw_intel_probe, "SOUNDWIRE_INTEL_INIT");
338
339/**
340 * sdw_intel_startup() - SoundWire Intel startup
341 * @ctx: SoundWire context allocated in the probe
342 *
343 * Startup Intel SoundWire controller. This function will be called after
344 * Intel Audio DSP is powered up.
345 */
346int sdw_intel_startup(struct sdw_intel_ctx *ctx)
347{
348 return sdw_intel_startup_controller(ctx);
349}
350EXPORT_SYMBOL_NS(sdw_intel_startup, "SOUNDWIRE_INTEL_INIT");
351/**
352 * sdw_intel_exit() - SoundWire Intel exit
353 * @ctx: SoundWire context allocated in the probe
354 *
355 * Delete the controller instances created and cleanup
356 */
357void sdw_intel_exit(struct sdw_intel_ctx *ctx)
358{
359 struct sdw_intel_link_res *link;
360
361 /* we first resume links and devices and wait synchronously before the cleanup */
362 list_for_each_entry(link, &ctx->link_list, list) {
363 struct sdw_bus *bus = &link->cdns->bus;
364 int ret;
365
366 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
367 if (ret < 0)
368 dev_err(bus->dev, "%s: intel_resume_child_device failed: %d\n",
369 __func__, ret);
370 }
371
372 sdw_intel_cleanup(ctx);
373 kfree(ctx->peripherals);
374 kfree(ctx->ldev);
375 kfree(ctx);
376}
377EXPORT_SYMBOL_NS(sdw_intel_exit, "SOUNDWIRE_INTEL_INIT");
378
379void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
380{
381 struct sdw_intel_link_dev *ldev;
382 u32 link_mask;
383 int i;
384
385 if (!ctx->ldev)
386 return;
387
388 link_mask = ctx->link_mask;
389
390 /* Startup SDW Master devices */
391 for (i = 0; i < ctx->count; i++) {
392 if (!(link_mask & BIT(i)))
393 continue;
394
395 ldev = ctx->ldev[i];
396
397 intel_link_process_wakeen_event(&ldev->auxdev);
398 }
399}
400EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, "SOUNDWIRE_INTEL_INIT");
401
402MODULE_LICENSE("Dual BSD/GPL");
403MODULE_DESCRIPTION("Intel Soundwire Init Library");
1// 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>
11#include <linux/platform_device.h>
12#include <linux/soundwire/sdw_intel.h>
13#include "intel.h"
14
15#define SDW_MAX_LINKS 4
16#define SDW_SHIM_LCAP 0x0
17#define SDW_SHIM_BASE 0x2C000
18#define SDW_ALH_BASE 0x2C800
19#define SDW_LINK_BASE 0x30000
20#define SDW_LINK_SIZE 0x10000
21
22struct sdw_link_data {
23 struct sdw_intel_link_res res;
24 struct platform_device *pdev;
25};
26
27struct sdw_intel_ctx {
28 int count;
29 struct sdw_link_data *links;
30};
31
32static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
33{
34 struct sdw_link_data *link = ctx->links;
35 int i;
36
37 if (!link)
38 return 0;
39
40 for (i = 0; i < ctx->count; i++) {
41 if (link->pdev)
42 platform_device_unregister(link->pdev);
43 link++;
44 }
45
46 kfree(ctx->links);
47 ctx->links = NULL;
48
49 return 0;
50}
51
52static struct sdw_intel_ctx
53*sdw_intel_add_controller(struct sdw_intel_res *res)
54{
55 struct platform_device_info pdevinfo;
56 struct platform_device *pdev;
57 struct sdw_link_data *link;
58 struct sdw_intel_ctx *ctx;
59 struct acpi_device *adev;
60 int ret, i;
61 u8 count;
62 u32 caps;
63
64 if (acpi_bus_get_device(res->handle, &adev))
65 return NULL;
66
67 /* Found controller, find links supported */
68 count = 0;
69 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
70 "mipi-sdw-master-count", &count, 1);
71
72 /* Don't fail on error, continue and use hw value */
73 if (ret) {
74 dev_err(&adev->dev,
75 "Failed to read mipi-sdw-master-count: %d\n", ret);
76 count = SDW_MAX_LINKS;
77 }
78
79 /* Check SNDWLCAP.LCOUNT */
80 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
81
82 /* Check HW supported vs property value and use min of two */
83 count = min_t(u8, caps, count);
84
85 /* Check count is within bounds */
86 if (count > SDW_MAX_LINKS) {
87 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
88 count, SDW_MAX_LINKS);
89 return NULL;
90 }
91
92 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
93
94 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
95 if (!ctx)
96 return NULL;
97
98 ctx->count = count;
99 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
100 if (!ctx->links)
101 goto link_err;
102
103 link = ctx->links;
104
105 /* Create SDW Master devices */
106 for (i = 0; i < count; i++) {
107
108 link->res.irq = res->irq;
109 link->res.registers = res->mmio_base + SDW_LINK_BASE
110 + (SDW_LINK_SIZE * i);
111 link->res.shim = res->mmio_base + SDW_SHIM_BASE;
112 link->res.alh = res->mmio_base + SDW_ALH_BASE;
113
114 memset(&pdevinfo, 0, sizeof(pdevinfo));
115
116 pdevinfo.parent = res->parent;
117 pdevinfo.name = "int-sdw";
118 pdevinfo.id = i;
119 pdevinfo.fwnode = acpi_fwnode_handle(adev);
120 pdevinfo.data = &link->res;
121 pdevinfo.size_data = sizeof(link->res);
122
123 pdev = platform_device_register_full(&pdevinfo);
124 if (IS_ERR(pdev)) {
125 dev_err(&adev->dev,
126 "platform device creation failed: %ld\n",
127 PTR_ERR(pdev));
128 goto pdev_err;
129 }
130
131 link->pdev = pdev;
132 link++;
133 }
134
135 return ctx;
136
137pdev_err:
138 sdw_intel_cleanup_pdev(ctx);
139link_err:
140 kfree(ctx);
141 return NULL;
142}
143
144static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
145 void *cdata, void **return_value)
146{
147 struct sdw_intel_res *res = cdata;
148 struct acpi_device *adev;
149
150 if (acpi_bus_get_device(handle, &adev)) {
151 dev_err(&adev->dev, "Couldn't find ACPI handle\n");
152 return AE_NOT_FOUND;
153 }
154
155 res->handle = handle;
156 return AE_OK;
157}
158
159/**
160 * sdw_intel_init() - SoundWire Intel init routine
161 * @parent_handle: ACPI parent handle
162 * @res: resource data
163 *
164 * This scans the namespace and creates SoundWire link controller devices
165 * based on the info queried.
166 */
167void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
168{
169 acpi_status status;
170
171 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
172 parent_handle, 1,
173 sdw_intel_acpi_cb,
174 NULL, res, NULL);
175 if (ACPI_FAILURE(status))
176 return NULL;
177
178 return sdw_intel_add_controller(res);
179}
180EXPORT_SYMBOL(sdw_intel_init);
181
182/**
183 * sdw_intel_exit() - SoundWire Intel exit
184 * @arg: callback context
185 *
186 * Delete the controller instances created and cleanup
187 */
188void sdw_intel_exit(void *arg)
189{
190 struct sdw_intel_ctx *ctx = arg;
191
192 sdw_intel_cleanup_pdev(ctx);
193 kfree(ctx);
194}
195EXPORT_SYMBOL(sdw_intel_exit);
196
197MODULE_LICENSE("Dual BSD/GPL");
198MODULE_DESCRIPTION("Intel Soundwire Init Library");