blob: 771a53a5c033b7b983108920064c556ce5c1838b [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
25struct sdw_link_data {
26 struct sdw_intel_link_res res;
27 struct platform_device *pdev;
28};
29
30struct sdw_intel_ctx {
31 int count;
32 struct sdw_link_data *links;
33};
34
35static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
36{
37 struct sdw_link_data *link = ctx->links;
38 int i;
39
40 if (!link)
41 return 0;
42
43 for (i = 0; i < ctx->count; i++) {
44 if (link->pdev)
45 platform_device_unregister(link->pdev);
46 link++;
47 }
48
49 kfree(ctx->links);
50 ctx->links = NULL;
51
52 return 0;
53}
54
55static struct sdw_intel_ctx
56*sdw_intel_add_controller(struct sdw_intel_res *res)
57{
58 struct platform_device_info pdevinfo;
59 struct platform_device *pdev;
60 struct sdw_link_data *link;
61 struct sdw_intel_ctx *ctx;
62 struct acpi_device *adev;
63 int ret, i;
64 u8 count;
65 u32 caps;
66
67 if (acpi_bus_get_device(res->handle, &adev))
68 return NULL;
69
70 /* Found controller, find links supported */
71 count = 0;
72 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050073 "mipi-sdw-master-count", &count, 1);
Vinod Kould62a7d42017-12-14 11:19:44 +053074
75 /* Don't fail on error, continue and use hw value */
76 if (ret) {
77 dev_err(&adev->dev,
78 "Failed to read mipi-sdw-master-count: %d\n", ret);
79 count = SDW_MAX_LINKS;
80 }
81
82 /* Check SNDWLCAP.LCOUNT */
83 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
84
85 /* Check HW supported vs property value and use min of two */
86 count = min_t(u8, caps, count);
87
88 /* Check count is within bounds */
89 if (count > SDW_MAX_LINKS) {
90 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050091 count, SDW_MAX_LINKS);
Vinod Kould62a7d42017-12-14 11:19:44 +053092 return NULL;
93 }
94
95 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
96
97 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
98 if (!ctx)
99 return NULL;
100
101 ctx->count = count;
102 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
103 if (!ctx->links)
104 goto link_err;
105
106 link = ctx->links;
107
108 /* Create SDW Master devices */
109 for (i = 0; i < count; i++) {
Vinod Kould62a7d42017-12-14 11:19:44 +0530110 link->res.irq = res->irq;
111 link->res.registers = res->mmio_base + SDW_LINK_BASE
112 + (SDW_LINK_SIZE * i);
113 link->res.shim = res->mmio_base + SDW_SHIM_BASE;
114 link->res.alh = res->mmio_base + SDW_ALH_BASE;
115
Vinod Koulc46302e2018-04-26 18:39:05 +0530116 link->res.ops = res->ops;
117 link->res.arg = res->arg;
118
Vinod Kould62a7d42017-12-14 11:19:44 +0530119 memset(&pdevinfo, 0, sizeof(pdevinfo));
120
121 pdevinfo.parent = res->parent;
122 pdevinfo.name = "int-sdw";
123 pdevinfo.id = i;
124 pdevinfo.fwnode = acpi_fwnode_handle(adev);
125 pdevinfo.data = &link->res;
126 pdevinfo.size_data = sizeof(link->res);
127
128 pdev = platform_device_register_full(&pdevinfo);
129 if (IS_ERR(pdev)) {
130 dev_err(&adev->dev,
131 "platform device creation failed: %ld\n",
132 PTR_ERR(pdev));
133 goto pdev_err;
134 }
135
136 link->pdev = pdev;
137 link++;
138 }
139
140 return ctx;
141
142pdev_err:
143 sdw_intel_cleanup_pdev(ctx);
144link_err:
145 kfree(ctx);
146 return NULL;
147}
148
149static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500150 void *cdata, void **return_value)
Vinod Kould62a7d42017-12-14 11:19:44 +0530151{
152 struct sdw_intel_res *res = cdata;
153 struct acpi_device *adev;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500154 acpi_status status;
155 u64 adr;
156
157 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
158 if (ACPI_FAILURE(status))
159 return AE_OK; /* keep going */
Vinod Kould62a7d42017-12-14 11:19:44 +0530160
161 if (acpi_bus_get_device(handle, &adev)) {
Vinod Koule1c815f2018-08-07 11:54:50 +0530162 pr_err("%s: Couldn't find ACPI handle\n", __func__);
Vinod Kould62a7d42017-12-14 11:19:44 +0530163 return AE_NOT_FOUND;
164 }
165
166 res->handle = handle;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500167
168 /*
169 * On some Intel platforms, multiple children of the HDAS
170 * device can be found, but only one of them is the SoundWire
171 * controller. The SNDW device is always exposed with
172 * Name(_ADR, 0x40000000), with bits 31..28 representing the
173 * SoundWire link so filter accordingly
174 */
175 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
176 return AE_OK; /* keep going */
177
178 /* device found, stop namespace walk */
179 return AE_CTRL_TERMINATE;
Vinod Kould62a7d42017-12-14 11:19:44 +0530180}
181
182/**
183 * sdw_intel_init() - SoundWire Intel init routine
184 * @parent_handle: ACPI parent handle
185 * @res: resource data
186 *
187 * This scans the namespace and creates SoundWire link controller devices
188 * based on the info queried.
189 */
190void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
191{
192 acpi_status status;
193
194 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500195 parent_handle, 1,
196 sdw_intel_acpi_cb,
197 NULL, res, NULL);
Vinod Kould62a7d42017-12-14 11:19:44 +0530198 if (ACPI_FAILURE(status))
199 return NULL;
200
201 return sdw_intel_add_controller(res);
202}
203EXPORT_SYMBOL(sdw_intel_init);
204
205/**
206 * sdw_intel_exit() - SoundWire Intel exit
207 * @arg: callback context
208 *
209 * Delete the controller instances created and cleanup
210 */
211void sdw_intel_exit(void *arg)
212{
213 struct sdw_intel_ctx *ctx = arg;
214
215 sdw_intel_cleanup_pdev(ctx);
216 kfree(ctx);
217}
218EXPORT_SYMBOL(sdw_intel_exit);
219
220MODULE_LICENSE("Dual BSD/GPL");
221MODULE_DESCRIPTION("Intel Soundwire Init Library");