Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 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> |
Paul Gortmaker | 4abbd78 | 2019-04-13 11:12:52 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Vinod Koul | 3fc4044 | 2019-10-22 10:03:08 +0530 | [diff] [blame] | 12 | #include <linux/io.h> |
Paul Gortmaker | 4abbd78 | 2019-04-13 11:12:52 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/soundwire/sdw_intel.h> |
| 16 | #include "intel.h" |
| 17 | |
Pierre-Louis Bossart | 6f11586 | 2019-05-22 14:47:17 -0500 | [diff] [blame] | 18 | #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */ |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 19 | #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 Bossart | 50302fc | 2019-08-05 19:55:20 -0500 | [diff] [blame] | 26 | static int link_mask; |
| 27 | module_param_named(sdw_link_mask, link_mask, int, 0444); |
| 28 | MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)"); |
| 29 | |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 30 | static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx) |
| 31 | { |
Pierre-Louis Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 32 | struct sdw_intel_link_res *link = ctx->links; |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 33 | 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 | |
| 50 | static 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 Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 55 | struct sdw_intel_link_res *link; |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 56 | 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 Bossart | 505ccb0 | 2019-05-01 10:57:37 -0500 | [diff] [blame] | 68 | "mipi-sdw-master-count", &count, 1); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 69 | |
| 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 Bossart | 432732b | 2019-05-22 14:47:31 -0500 | [diff] [blame] | 79 | caps &= GENMASK(2, 0); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 80 | |
| 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 Bossart | 505ccb0 | 2019-05-01 10:57:37 -0500 | [diff] [blame] | 87 | count, SDW_MAX_LINKS); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 88 | return NULL; |
Pierre-Louis Bossart | 432732b | 2019-05-22 14:47:31 -0500 | [diff] [blame] | 89 | } else if (!count) { |
| 90 | dev_warn(&adev->dev, "No SoundWire links detected\n"); |
| 91 | return NULL; |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 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++) { |
Pierre-Louis Bossart | 50302fc | 2019-08-05 19:55:20 -0500 | [diff] [blame] | 109 | if (link_mask && !(link_mask & BIT(i))) { |
| 110 | dev_dbg(&adev->dev, |
| 111 | "Link %d masked, will not be enabled\n", i); |
| 112 | link++; |
| 113 | continue; |
| 114 | } |
| 115 | |
Pierre-Louis Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 116 | link->registers = res->mmio_base + SDW_LINK_BASE |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 117 | + (SDW_LINK_SIZE * i); |
Pierre-Louis Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 118 | link->shim = res->mmio_base + SDW_SHIM_BASE; |
| 119 | link->alh = res->mmio_base + SDW_ALH_BASE; |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 120 | |
Pierre-Louis Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 121 | link->ops = res->ops; |
Rander Wang | 4b206d3 | 2019-12-11 19:45:02 -0600 | [diff] [blame^] | 122 | link->dev = res->dev; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 123 | |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 124 | memset(&pdevinfo, 0, sizeof(pdevinfo)); |
| 125 | |
| 126 | pdevinfo.parent = res->parent; |
| 127 | pdevinfo.name = "int-sdw"; |
| 128 | pdevinfo.id = i; |
| 129 | pdevinfo.fwnode = acpi_fwnode_handle(adev); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 130 | |
| 131 | pdev = platform_device_register_full(&pdevinfo); |
| 132 | if (IS_ERR(pdev)) { |
| 133 | dev_err(&adev->dev, |
| 134 | "platform device creation failed: %ld\n", |
| 135 | PTR_ERR(pdev)); |
| 136 | goto pdev_err; |
| 137 | } |
| 138 | |
| 139 | link->pdev = pdev; |
| 140 | link++; |
| 141 | } |
| 142 | |
| 143 | return ctx; |
| 144 | |
| 145 | pdev_err: |
| 146 | sdw_intel_cleanup_pdev(ctx); |
| 147 | link_err: |
| 148 | kfree(ctx); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level, |
Pierre-Louis Bossart | 505ccb0 | 2019-05-01 10:57:37 -0500 | [diff] [blame] | 153 | void *cdata, void **return_value) |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 154 | { |
| 155 | struct sdw_intel_res *res = cdata; |
| 156 | struct acpi_device *adev; |
Pierre-Louis Bossart | 6f11586 | 2019-05-22 14:47:17 -0500 | [diff] [blame] | 157 | acpi_status status; |
| 158 | u64 adr; |
| 159 | |
| 160 | status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr); |
| 161 | if (ACPI_FAILURE(status)) |
| 162 | return AE_OK; /* keep going */ |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 163 | |
| 164 | if (acpi_bus_get_device(handle, &adev)) { |
Vinod Koul | e1c815f | 2018-08-07 11:54:50 +0530 | [diff] [blame] | 165 | pr_err("%s: Couldn't find ACPI handle\n", __func__); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 166 | return AE_NOT_FOUND; |
| 167 | } |
| 168 | |
| 169 | res->handle = handle; |
Pierre-Louis Bossart | 6f11586 | 2019-05-22 14:47:17 -0500 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * On some Intel platforms, multiple children of the HDAS |
| 173 | * device can be found, but only one of them is the SoundWire |
| 174 | * controller. The SNDW device is always exposed with |
| 175 | * Name(_ADR, 0x40000000), with bits 31..28 representing the |
| 176 | * SoundWire link so filter accordingly |
| 177 | */ |
| 178 | if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE) |
| 179 | return AE_OK; /* keep going */ |
| 180 | |
| 181 | /* device found, stop namespace walk */ |
| 182 | return AE_CTRL_TERMINATE; |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * sdw_intel_init() - SoundWire Intel init routine |
| 187 | * @parent_handle: ACPI parent handle |
| 188 | * @res: resource data |
| 189 | * |
| 190 | * This scans the namespace and creates SoundWire link controller devices |
| 191 | * based on the info queried. |
| 192 | */ |
| 193 | void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res) |
| 194 | { |
| 195 | acpi_status status; |
| 196 | |
| 197 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, |
Pierre-Louis Bossart | 505ccb0 | 2019-05-01 10:57:37 -0500 | [diff] [blame] | 198 | parent_handle, 1, |
| 199 | sdw_intel_acpi_cb, |
| 200 | NULL, res, NULL); |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 201 | if (ACPI_FAILURE(status)) |
| 202 | return NULL; |
| 203 | |
| 204 | return sdw_intel_add_controller(res); |
| 205 | } |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * sdw_intel_exit() - SoundWire Intel exit |
| 209 | * @arg: callback context |
| 210 | * |
| 211 | * Delete the controller instances created and cleanup |
| 212 | */ |
Pierre-Louis Bossart | f98f690 | 2019-12-11 19:45:01 -0600 | [diff] [blame] | 213 | void sdw_intel_exit(struct sdw_intel_ctx *ctx) |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 214 | { |
Vinod Koul | d62a7d4 | 2017-12-14 11:19:44 +0530 | [diff] [blame] | 215 | sdw_intel_cleanup_pdev(ctx); |
| 216 | kfree(ctx); |
| 217 | } |
| 218 | EXPORT_SYMBOL(sdw_intel_exit); |
| 219 | |
| 220 | MODULE_LICENSE("Dual BSD/GPL"); |
| 221 | MODULE_DESCRIPTION("Intel Soundwire Init Library"); |