intel_init.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. /*
  4. * SDW Intel Init Routines
  5. *
  6. * Initializes and creates SDW devices based on ACPI and Hardware values
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/soundwire/sdw_intel.h>
  11. #include "intel.h"
  12. #define SDW_MAX_LINKS 4
  13. #define SDW_SHIM_LCAP 0x0
  14. #define SDW_SHIM_BASE 0x2C000
  15. #define SDW_ALH_BASE 0x2C800
  16. #define SDW_LINK_BASE 0x30000
  17. #define SDW_LINK_SIZE 0x10000
  18. struct sdw_link_data {
  19. struct sdw_intel_link_res res;
  20. struct platform_device *pdev;
  21. };
  22. struct sdw_intel_ctx {
  23. int count;
  24. struct sdw_link_data *links;
  25. };
  26. static int sdw_intel_cleanup_pdev(struct sdw_intel_ctx *ctx)
  27. {
  28. struct sdw_link_data *link = ctx->links;
  29. int i;
  30. if (!link)
  31. return 0;
  32. for (i = 0; i < ctx->count; i++) {
  33. if (link->pdev)
  34. platform_device_unregister(link->pdev);
  35. link++;
  36. }
  37. kfree(ctx->links);
  38. ctx->links = NULL;
  39. return 0;
  40. }
  41. static struct sdw_intel_ctx
  42. *sdw_intel_add_controller(struct sdw_intel_res *res)
  43. {
  44. struct platform_device_info pdevinfo;
  45. struct platform_device *pdev;
  46. struct sdw_link_data *link;
  47. struct sdw_intel_ctx *ctx;
  48. struct acpi_device *adev;
  49. int ret, i;
  50. u8 count;
  51. u32 caps;
  52. if (acpi_bus_get_device(res->handle, &adev))
  53. return NULL;
  54. /* Found controller, find links supported */
  55. count = 0;
  56. ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
  57. "mipi-sdw-master-count", &count, 1);
  58. /* Don't fail on error, continue and use hw value */
  59. if (ret) {
  60. dev_err(&adev->dev,
  61. "Failed to read mipi-sdw-master-count: %d\n", ret);
  62. count = SDW_MAX_LINKS;
  63. }
  64. /* Check SNDWLCAP.LCOUNT */
  65. caps = ioread32(res->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
  66. /* Check HW supported vs property value and use min of two */
  67. count = min_t(u8, caps, count);
  68. /* Check count is within bounds */
  69. if (count > SDW_MAX_LINKS) {
  70. dev_err(&adev->dev, "Link count %d exceeds max %d\n",
  71. count, SDW_MAX_LINKS);
  72. return NULL;
  73. }
  74. dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
  75. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  76. if (!ctx)
  77. return NULL;
  78. ctx->count = count;
  79. ctx->links = kcalloc(ctx->count, sizeof(*ctx->links), GFP_KERNEL);
  80. if (!ctx->links)
  81. goto link_err;
  82. link = ctx->links;
  83. /* Create SDW Master devices */
  84. for (i = 0; i < count; i++) {
  85. link->res.irq = res->irq;
  86. link->res.registers = res->mmio_base + SDW_LINK_BASE
  87. + (SDW_LINK_SIZE * i);
  88. link->res.shim = res->mmio_base + SDW_SHIM_BASE;
  89. link->res.alh = res->mmio_base + SDW_ALH_BASE;
  90. link->res.ops = res->ops;
  91. link->res.arg = res->arg;
  92. memset(&pdevinfo, 0, sizeof(pdevinfo));
  93. pdevinfo.parent = res->parent;
  94. pdevinfo.name = "int-sdw";
  95. pdevinfo.id = i;
  96. pdevinfo.fwnode = acpi_fwnode_handle(adev);
  97. pdevinfo.data = &link->res;
  98. pdevinfo.size_data = sizeof(link->res);
  99. pdev = platform_device_register_full(&pdevinfo);
  100. if (IS_ERR(pdev)) {
  101. dev_err(&adev->dev,
  102. "platform device creation failed: %ld\n",
  103. PTR_ERR(pdev));
  104. goto pdev_err;
  105. }
  106. link->pdev = pdev;
  107. link++;
  108. }
  109. return ctx;
  110. pdev_err:
  111. sdw_intel_cleanup_pdev(ctx);
  112. link_err:
  113. kfree(ctx);
  114. return NULL;
  115. }
  116. static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
  117. void *cdata, void **return_value)
  118. {
  119. struct sdw_intel_res *res = cdata;
  120. struct acpi_device *adev;
  121. if (acpi_bus_get_device(handle, &adev)) {
  122. pr_err("%s: Couldn't find ACPI handle\n", __func__);
  123. return AE_NOT_FOUND;
  124. }
  125. res->handle = handle;
  126. return AE_OK;
  127. }
  128. /**
  129. * sdw_intel_init() - SoundWire Intel init routine
  130. * @parent_handle: ACPI parent handle
  131. * @res: resource data
  132. *
  133. * This scans the namespace and creates SoundWire link controller devices
  134. * based on the info queried.
  135. */
  136. void *sdw_intel_init(acpi_handle *parent_handle, struct sdw_intel_res *res)
  137. {
  138. acpi_status status;
  139. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  140. parent_handle, 1,
  141. sdw_intel_acpi_cb,
  142. NULL, res, NULL);
  143. if (ACPI_FAILURE(status))
  144. return NULL;
  145. return sdw_intel_add_controller(res);
  146. }
  147. EXPORT_SYMBOL(sdw_intel_init);
  148. /**
  149. * sdw_intel_exit() - SoundWire Intel exit
  150. * @arg: callback context
  151. *
  152. * Delete the controller instances created and cleanup
  153. */
  154. void sdw_intel_exit(void *arg)
  155. {
  156. struct sdw_intel_ctx *ctx = arg;
  157. sdw_intel_cleanup_pdev(ctx);
  158. kfree(ctx);
  159. }
  160. EXPORT_SYMBOL(sdw_intel_exit);
  161. MODULE_LICENSE("Dual BSD/GPL");
  162. MODULE_DESCRIPTION("Intel Soundwire Init Library");