blob: e0f2903101c7461389bc58e96336eecf2e8cd277 [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
17#define SDW_MAX_LINKS 4
18#define SDW_SHIM_LCAP 0x0
19#define SDW_SHIM_BASE 0x2C000
20#define SDW_ALH_BASE 0x2C800
21#define SDW_LINK_BASE 0x30000
22#define SDW_LINK_SIZE 0x10000
23
24struct sdw_link_data {
25 struct sdw_intel_link_res res;
26 struct platform_device *pdev;
27};
28
29struct sdw_intel_ctx {
30 int count;
31 struct sdw_link_data *links;
32};
33
34static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
35{
36 struct sdw_link_data *link = ctx->links;
37 int i;
38
39 if (!link)
40 return 0;
41
42 for (i = 0; i < ctx->count; i++) {
43 if (link->pdev)
44 platform_device_unregister(link->pdev);
45 link++;
46 }
47
48 kfree(ctx->links);
49 ctx->links = NULL;
50
51 return 0;
52}
53
54static struct sdw_intel_ctx
55*sdw_intel_add_controller(struct sdw_intel_res *res)
56{
57 struct platform_device_info pdevinfo;
58 struct platform_device *pdev;
59 struct sdw_link_data *link;
60 struct sdw_intel_ctx *ctx;
61 struct acpi_device *adev;
62 int ret, i;
63 u8 count;
64 u32 caps;
65
66 if (acpi_bus_get_device(res->handle, &adev))
67 return NULL;
68
69 /* Found controller, find links supported */
70 count = 0;
71 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
72 "mipi-sdw-master-count", &count, 1);
73
74 /* Don't fail on error, continue and use hw value */
75 if (ret) {
76 dev_err(&adev->dev,
77 "Failed to read mipi-sdw-master-count: %d\n", ret);
78 count = SDW_MAX_LINKS;
79 }
80
81 /* Check SNDWLCAP.LCOUNT */
82 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
83
84 /* Check HW supported vs property value and use min of two */
85 count = min_t(u8, caps, count);
86
87 /* Check count is within bounds */
88 if (count > SDW_MAX_LINKS) {
89 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
90 count, SDW_MAX_LINKS);
91 return NULL;
92 }
93
94 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
95
96 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
97 if (!ctx)
98 return NULL;
99
100 ctx->count = count;
101 ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
102 if (!ctx->links)
103 goto link_err;
104
105 link = ctx->links;
106
107 /* Create SDW Master devices */
108 for (i = 0; i < count; i++) {
109
110 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,
150 void *cdata, void **return_value)
151{
152 struct sdw_intel_res *res = cdata;
153 struct acpi_device *adev;
154
155 if (acpi_bus_get_device(handle, &adev)) {
Vinod Koule1c815f2018-08-07 11:54:50 +0530156 pr_err("%s: Couldn't find ACPI handle\n", __func__);
Vinod Kould62a7d42017-12-14 11:19:44 +0530157 return AE_NOT_FOUND;
158 }
159
160 res->handle = handle;
161 return AE_OK;
162}
163
164/**
165 * sdw_intel_init() - SoundWire Intel init routine
166 * @parent_handle: ACPI parent handle
167 * @res: resource data
168 *
169 * This scans the namespace and creates SoundWire link controller devices
170 * based on the info queried.
171 */
172void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
173{
174 acpi_status status;
175
176 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
177 parent_handle, 1,
178 sdw_intel_acpi_cb,
179 NULL, res, NULL);
180 if (ACPI_FAILURE(status))
181 return NULL;
182
183 return sdw_intel_add_controller(res);
184}
185EXPORT_SYMBOL(sdw_intel_init);
186
187/**
188 * sdw_intel_exit() - SoundWire Intel exit
189 * @arg: callback context
190 *
191 * Delete the controller instances created and cleanup
192 */
193void sdw_intel_exit(void *arg)
194{
195 struct sdw_intel_ctx *ctx = arg;
196
197 sdw_intel_cleanup_pdev(ctx);
198 kfree(ctx);
199}
200EXPORT_SYMBOL(sdw_intel_exit);
201
202MODULE_LICENSE("Dual BSD/GPL");
203MODULE_DESCRIPTION("Intel Soundwire Init Library");