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/module.h> 13 #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 24 struct sdw_link_data { 25 struct sdw_intel_link_res res; 26 struct platform_device *pdev; 27 }; 28 29 struct sdw_intel_ctx { 30 int count; 31 struct sdw_link_data *links; 32 }; 33 34 static 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 54 static 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 link->res.irq = res->irq; 110 link->res.registers = res->mmio_base + SDW_LINK_BASE 111 + (SDW_LINK_SIZE * i); 112 link->res.shim = res->mmio_base + SDW_SHIM_BASE; 113 link->res.alh = res->mmio_base + SDW_ALH_BASE; 114 115 link->res.ops = res->ops; 116 link->res.arg = res->arg; 117 118 memset(&pdevinfo, 0, sizeof(pdevinfo)); 119 120 pdevinfo.parent = res->parent; 121 pdevinfo.name = "int-sdw"; 122 pdevinfo.id = i; 123 pdevinfo.fwnode = acpi_fwnode_handle(adev); 124 pdevinfo.data = &link->res; 125 pdevinfo.size_data = sizeof(link->res); 126 127 pdev = platform_device_register_full(&pdevinfo); 128 if (IS_ERR(pdev)) { 129 dev_err(&adev->dev, 130 "platform device creation failed: %ld\n", 131 PTR_ERR(pdev)); 132 goto pdev_err; 133 } 134 135 link->pdev = pdev; 136 link++; 137 } 138 139 return ctx; 140 141 pdev_err: 142 sdw_intel_cleanup_pdev(ctx); 143 link_err: 144 kfree(ctx); 145 return NULL; 146 } 147 148 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level, 149 void *cdata, void **return_value) 150 { 151 struct sdw_intel_res *res = cdata; 152 struct acpi_device *adev; 153 154 if (acpi_bus_get_device(handle, &adev)) { 155 pr_err("%s: Couldn't find ACPI handle\n", __func__); 156 return AE_NOT_FOUND; 157 } 158 159 res->handle = handle; 160 return AE_OK; 161 } 162 163 /** 164 * sdw_intel_init() - SoundWire Intel init routine 165 * @parent_handle: ACPI parent handle 166 * @res: resource data 167 * 168 * This scans the namespace and creates SoundWire link controller devices 169 * based on the info queried. 170 */ 171 void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res) 172 { 173 acpi_status status; 174 175 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, 176 parent_handle, 1, 177 sdw_intel_acpi_cb, 178 NULL, res, NULL); 179 if (ACPI_FAILURE(status)) 180 return NULL; 181 182 return sdw_intel_add_controller(res); 183 } 184 EXPORT_SYMBOL(sdw_intel_init); 185 186 /** 187 * sdw_intel_exit() - SoundWire Intel exit 188 * @arg: callback context 189 * 190 * Delete the controller instances created and cleanup 191 */ 192 void sdw_intel_exit(void *arg) 193 { 194 struct sdw_intel_ctx *ctx = arg; 195 196 sdw_intel_cleanup_pdev(ctx); 197 kfree(ctx); 198 } 199 EXPORT_SYMBOL(sdw_intel_exit); 200 201 MODULE_LICENSE("Dual BSD/GPL"); 202 MODULE_DESCRIPTION("Intel Soundwire Init Library"); 203