blob: d5d42795a48fa4ce21fbc077e996c7a131af0535 [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>
Vinod Koul3fc40442019-10-22 10:03:08 +053012#include <linux/io.h>
Paul Gortmaker4abbd782019-04-13 11:12:52 -040013#include <linux/module.h>
Vinod Kould62a7d42017-12-14 11:19:44 +053014#include <linux/platform_device.h>
15#include <linux/soundwire/sdw_intel.h>
16#include "intel.h"
17
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -050018#define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */
Vinod Kould62a7d42017-12-14 11:19:44 +053019#define SDW_MAX_LINKS 4
20#define SDW_SHIM_LCAP 0x0
21#define SDW_SHIM_BASE 0x2C000
22#define SDW_ALH_BASE 0x2C800
23#define SDW_LINK_BASE 0x30000
24#define SDW_LINK_SIZE 0x10000
25
Pierre-Louis Bossart50302fc2019-08-05 19:55:20 -050026static int link_mask;
27module_param_named(sdw_link_mask, link_mask, int, 0444);
28MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
29
Vinod Kould62a7d42017-12-14 11:19:44 +053030static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
31{
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -060032 struct sdw_intel_link_res *link = ctx->links;
Vinod Kould62a7d42017-12-14 11:19:44 +053033 int i;
34
35 if (!link)
36 return 0;
37
38 for (i = 0; i < ctx->count; i++) {
39 if (link->pdev)
40 platform_device_unregister(link->pdev);
41 link++;
42 }
43
44 kfree(ctx->links);
45 ctx->links = NULL;
46
47 return 0;
48}
49
50static struct sdw_intel_ctx
51*sdw_intel_add_controller(struct sdw_intel_res *res)
52{
53 struct platform_device_info pdevinfo;
54 struct platform_device *pdev;
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -060055 struct sdw_intel_link_res *link;
Vinod Kould62a7d42017-12-14 11:19:44 +053056 struct sdw_intel_ctx *ctx;
57 struct acpi_device *adev;
58 int ret, i;
59 u8 count;
60 u32 caps;
61
62 if (acpi_bus_get_device(res->handle, &adev))
63 return NULL;
64
65 /* Found controller, find links supported */
66 count = 0;
67 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050068 "mipi-sdw-master-count", &count, 1);
Vinod Kould62a7d42017-12-14 11:19:44 +053069
70 /* Don't fail on error, continue and use hw value */
71 if (ret) {
72 dev_err(&adev->dev,
73 "Failed to read mipi-sdw-master-count: %d\n", ret);
74 count = SDW_MAX_LINKS;
75 }
76
77 /* Check SNDWLCAP.LCOUNT */
78 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
Pierre-Louis Bossart432732b2019-05-22 14:47:31 -050079 caps &= GENMASK(2, 0);
Vinod Kould62a7d42017-12-14 11:19:44 +053080
81 /* Check HW supported vs property value and use min of two */
82 count = min_t(u8, caps, count);
83
84 /* Check count is within bounds */
85 if (count > SDW_MAX_LINKS) {
86 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -050087 count, SDW_MAX_LINKS);
Vinod Kould62a7d42017-12-14 11:19:44 +053088 return NULL;
Guennadi Liakhovetski6f7219f2020-05-08 02:30:46 +020089 }
90
91 if (!count) {
Pierre-Louis Bossart432732b2019-05-22 14:47:31 -050092 dev_warn(&adev->dev, "No SoundWire links detected\n");
93 return NULL;
Vinod Kould62a7d42017-12-14 11:19:44 +053094 }
95
96 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
97
98 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
99 if (!ctx)
100 return NULL;
101
102 ctx->count = count;
103 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
104 if (!ctx->links)
105 goto link_err;
106
107 link = ctx->links;
108
109 /* Create SDW Master devices */
110 for (i = 0; i < count; i++) {
Pierre-Louis Bossart50302fc2019-08-05 19:55:20 -0500111 if (link_mask && !(link_mask & BIT(i))) {
112 dev_dbg(&adev->dev,
113 "Link %d masked, will not be enabled\n", i);
114 link++;
115 continue;
116 }
117
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -0600118 link->registers = res->mmio_base + SDW_LINK_BASE
Vinod Kould62a7d42017-12-14 11:19:44 +0530119 + (SDW_LINK_SIZE * i);
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -0600120 link->shim = res->mmio_base + SDW_SHIM_BASE;
121 link->alh = res->mmio_base + SDW_ALH_BASE;
Vinod Kould62a7d42017-12-14 11:19:44 +0530122
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -0600123 link->ops = res->ops;
Rander Wang4b206d32019-12-11 19:45:02 -0600124 link->dev = res->dev;
Vinod Koulc46302e2018-04-26 18:39:05 +0530125
Vinod Kould62a7d42017-12-14 11:19:44 +0530126 memset(&pdevinfo, 0, sizeof(pdevinfo));
127
128 pdevinfo.parent = res->parent;
129 pdevinfo.name = "int-sdw";
130 pdevinfo.id = i;
131 pdevinfo.fwnode = acpi_fwnode_handle(adev);
Vinod Kould62a7d42017-12-14 11:19:44 +0530132
133 pdev = platform_device_register_full(&pdevinfo);
134 if (IS_ERR(pdev)) {
135 dev_err(&adev->dev,
136 "platform device creation failed: %ld\n",
137 PTR_ERR(pdev));
138 goto pdev_err;
139 }
140
141 link->pdev = pdev;
142 link++;
143 }
144
145 return ctx;
146
147pdev_err:
148 sdw_intel_cleanup_pdev(ctx);
149link_err:
150 kfree(ctx);
151 return NULL;
152}
153
154static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500155 void *cdata, void **return_value)
Vinod Kould62a7d42017-12-14 11:19:44 +0530156{
157 struct sdw_intel_res *res = cdata;
158 struct acpi_device *adev;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500159 acpi_status status;
160 u64 adr;
161
162 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
163 if (ACPI_FAILURE(status))
164 return AE_OK; /* keep going */
Vinod Kould62a7d42017-12-14 11:19:44 +0530165
166 if (acpi_bus_get_device(handle, &adev)) {
Vinod Koule1c815f2018-08-07 11:54:50 +0530167 pr_err("%s: Couldn't find ACPI handle\n", __func__);
Vinod Kould62a7d42017-12-14 11:19:44 +0530168 return AE_NOT_FOUND;
169 }
170
171 res->handle = handle;
Pierre-Louis Bossart6f115862019-05-22 14:47:17 -0500172
173 /*
174 * On some Intel platforms, multiple children of the HDAS
175 * device can be found, but only one of them is the SoundWire
176 * controller. The SNDW device is always exposed with
177 * Name(_ADR, 0x40000000), with bits 31..28 representing the
178 * SoundWire link so filter accordingly
179 */
180 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
181 return AE_OK; /* keep going */
182
183 /* device found, stop namespace walk */
184 return AE_CTRL_TERMINATE;
Vinod Kould62a7d42017-12-14 11:19:44 +0530185}
186
187/**
188 * sdw_intel_init() - SoundWire Intel init routine
189 * @parent_handle: ACPI parent handle
190 * @res: resource data
191 *
192 * This scans the namespace and creates SoundWire link controller devices
193 * based on the info queried.
194 */
195void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
196{
197 acpi_status status;
198
199 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
Pierre-Louis Bossart505ccb02019-05-01 10:57:37 -0500200 parent_handle, 1,
201 sdw_intel_acpi_cb,
202 NULL, res, NULL);
Vinod Kould62a7d42017-12-14 11:19:44 +0530203 if (ACPI_FAILURE(status))
204 return NULL;
205
206 return sdw_intel_add_controller(res);
207}
Vinod Kould62a7d42017-12-14 11:19:44 +0530208
209/**
210 * sdw_intel_exit() - SoundWire Intel exit
211 * @arg: callback context
212 *
213 * Delete the controller instances created and cleanup
214 */
Pierre-Louis Bossartf98f6902019-12-11 19:45:01 -0600215void sdw_intel_exit(struct sdw_intel_ctx *ctx)
Vinod Kould62a7d42017-12-14 11:19:44 +0530216{
Vinod Kould62a7d42017-12-14 11:19:44 +0530217 sdw_intel_cleanup_pdev(ctx);
218 kfree(ctx);
219}
220EXPORT_SYMBOL(sdw_intel_exit);
221
222MODULE_LICENSE("Dual BSD/GPL");
223MODULE_DESCRIPTION("Intel Soundwire Init Library");