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/io.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/soundwire/sdw_intel.h> 16 #include "cadence_master.h" 17 #include "intel.h" 18 19 #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */ 20 #define SDW_MAX_LINKS 4 21 #define SDW_SHIM_LCAP 0x0 22 #define SDW_SHIM_BASE 0x2C000 23 #define SDW_ALH_BASE 0x2C800 24 #define SDW_LINK_BASE 0x30000 25 #define SDW_LINK_SIZE 0x10000 26 27 static int link_mask; 28 module_param_named(sdw_link_mask, link_mask, int, 0444); 29 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)"); 30 31 static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx) 32 { 33 struct sdw_intel_link_res *link = ctx->links; 34 int i; 35 36 if (!link) 37 return 0; 38 39 for (i = 0; i < ctx->count; i++) { 40 if (link->pdev) 41 platform_device_unregister(link->pdev); 42 link++; 43 } 44 45 return 0; 46 } 47 48 static struct sdw_intel_ctx 49 *sdw_intel_add_controller(struct sdw_intel_res *res) 50 { 51 struct platform_device_info pdevinfo; 52 struct platform_device *pdev; 53 struct sdw_intel_link_res *link; 54 struct sdw_intel_ctx *ctx; 55 struct acpi_device *adev; 56 int ret, i; 57 u8 count; 58 u32 caps; 59 60 if (acpi_bus_get_device(res->handle, &adev)) 61 return NULL; 62 63 /* Found controller, find links supported */ 64 count = 0; 65 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev), 66 "mipi-sdw-master-count", &count, 1); 67 68 /* Don't fail on error, continue and use hw value */ 69 if (ret) { 70 dev_err(&adev->dev, 71 "Failed to read mipi-sdw-master-count: %d\n", ret); 72 count = SDW_MAX_LINKS; 73 } 74 75 /* Check SNDWLCAP.LCOUNT */ 76 caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP); 77 caps &= GENMASK(2, 0); 78 79 /* Check HW supported vs property value and use min of two */ 80 count = min_t(u8, caps, count); 81 82 /* Check count is within bounds */ 83 if (count > SDW_MAX_LINKS) { 84 dev_err(&adev->dev, "Link count %d exceeds max %d\n", 85 count, SDW_MAX_LINKS); 86 return NULL; 87 } 88 89 if (!count) { 90 dev_warn(&adev->dev, "No SoundWire links detected\n"); 91 return NULL; 92 } 93 94 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count); 95 96 ctx = devm_kzalloc(&adev->dev, sizeof(*ctx), GFP_KERNEL); 97 if (!ctx) 98 return NULL; 99 100 ctx->count = count; 101 ctx->links = devm_kcalloc(&adev->dev, ctx->count, 102 sizeof(*ctx->links), GFP_KERNEL); 103 if (!ctx->links) 104 return NULL; 105 106 link = ctx->links; 107 108 /* Create SDW Master devices */ 109 for (i = 0; i < count; i++) { 110 if (!(link_mask & BIT(i))) { 111 dev_dbg(&adev->dev, 112 "Link %d masked, will not be enabled\n", i); 113 link++; 114 continue; 115 } 116 117 link->registers = res->mmio_base + SDW_LINK_BASE 118 + (SDW_LINK_SIZE * i); 119 link->shim = res->mmio_base + SDW_SHIM_BASE; 120 link->alh = res->mmio_base + SDW_ALH_BASE; 121 122 link->ops = res->ops; 123 link->dev = res->dev; 124 125 memset(&pdevinfo, 0, sizeof(pdevinfo)); 126 127 pdevinfo.parent = res->parent; 128 pdevinfo.name = "int-sdw"; 129 pdevinfo.id = i; 130 pdevinfo.fwnode = acpi_fwnode_handle(adev); 131 pdevinfo.data = link; 132 pdevinfo.size_data = sizeof(*link); 133 134 pdev = platform_device_register_full(&pdevinfo); 135 if (IS_ERR(pdev)) { 136 dev_err(&adev->dev, 137 "platform device creation failed: %ld\n", 138 PTR_ERR(pdev)); 139 goto pdev_err; 140 } 141 142 link->pdev = pdev; 143 link++; 144 } 145 146 return ctx; 147 148 pdev_err: 149 ctx->count = i; 150 sdw_intel_cleanup_pdev(ctx); 151 return NULL; 152 } 153 154 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level, 155 void *cdata, void **return_value) 156 { 157 struct sdw_intel_res *res = cdata; 158 struct acpi_device *adev; 159 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 */ 165 166 if (acpi_bus_get_device(handle, &adev)) { 167 pr_err("%s: Couldn't find ACPI handle\n", __func__); 168 return AE_NOT_FOUND; 169 } 170 171 res->handle = handle; 172 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; 185 } 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 */ 195 void *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, 200 parent_handle, 1, 201 sdw_intel_acpi_cb, 202 NULL, res, NULL); 203 if (ACPI_FAILURE(status)) 204 return NULL; 205 206 return sdw_intel_add_controller(res); 207 } 208 209 /** 210 * sdw_intel_exit() - SoundWire Intel exit 211 * @arg: callback context 212 * 213 * Delete the controller instances created and cleanup 214 */ 215 void sdw_intel_exit(struct sdw_intel_ctx *ctx) 216 { 217 sdw_intel_cleanup_pdev(ctx); 218 } 219 EXPORT_SYMBOL(sdw_intel_exit); 220 221 MODULE_LICENSE("Dual BSD/GPL"); 222 MODULE_DESCRIPTION("Intel Soundwire Init Library"); 223