amd_init.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * SoundWire AMD Manager Initialize routines
  4. *
  5. * Initializes and creates SDW devices based on ACPI and Hardware values
  6. *
  7. * Copyright 2024 Advanced Micro Devices, Inc.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/export.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include "amd_init.h"
  16. #define ACP_PAD_PULLDOWN_CTRL 0x0001448
  17. #define ACP_SW_PAD_KEEPER_EN 0x0001454
  18. #define AMD_SDW0_PAD_CTRL_MASK 0x60
  19. #define AMD_SDW1_PAD_CTRL_MASK 5
  20. #define AMD_SDW_PAD_CTRL_MASK (AMD_SDW0_PAD_CTRL_MASK | AMD_SDW1_PAD_CTRL_MASK)
  21. #define AMD_SDW0_PAD_EN 1
  22. #define AMD_SDW1_PAD_EN 0x10
  23. #define AMD_SDW_PAD_EN (AMD_SDW0_PAD_EN | AMD_SDW1_PAD_EN)
  24. static int amd_enable_sdw_pads(void __iomem *mmio, u32 link_mask, struct device *dev)
  25. {
  26. u32 pad_keeper_en, pad_pulldown_ctrl_mask;
  27. switch (link_mask) {
  28. case 1:
  29. pad_keeper_en = AMD_SDW0_PAD_EN;
  30. pad_pulldown_ctrl_mask = AMD_SDW0_PAD_CTRL_MASK;
  31. break;
  32. case 2:
  33. pad_keeper_en = AMD_SDW1_PAD_EN;
  34. pad_pulldown_ctrl_mask = AMD_SDW1_PAD_CTRL_MASK;
  35. break;
  36. case 3:
  37. pad_keeper_en = AMD_SDW_PAD_EN;
  38. pad_pulldown_ctrl_mask = AMD_SDW_PAD_CTRL_MASK;
  39. break;
  40. default:
  41. dev_err(dev, "No SDW Links are enabled\n");
  42. return -ENODEV;
  43. }
  44. amd_updatel(mmio, ACP_SW_PAD_KEEPER_EN, pad_keeper_en, pad_keeper_en);
  45. amd_updatel(mmio, ACP_PAD_PULLDOWN_CTRL, pad_pulldown_ctrl_mask, 0);
  46. return 0;
  47. }
  48. static int sdw_amd_cleanup(struct sdw_amd_ctx *ctx)
  49. {
  50. int i;
  51. for (i = 0; i < ctx->count; i++) {
  52. if (!(ctx->link_mask & BIT(i)))
  53. continue;
  54. platform_device_unregister(ctx->pdev[i]);
  55. }
  56. return 0;
  57. }
  58. static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res)
  59. {
  60. struct sdw_amd_ctx *ctx;
  61. struct acpi_device *adev;
  62. struct acp_sdw_pdata sdw_pdata[2];
  63. struct platform_device_info pdevinfo[2];
  64. u32 link_mask;
  65. int count, index;
  66. int ret;
  67. if (!res)
  68. return NULL;
  69. adev = acpi_fetch_acpi_dev(res->handle);
  70. if (!adev)
  71. return NULL;
  72. if (!res->count)
  73. return NULL;
  74. count = res->count;
  75. dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
  76. ret = amd_enable_sdw_pads(res->mmio_base, res->link_mask, res->parent);
  77. if (ret)
  78. return NULL;
  79. /*
  80. * we need to alloc/free memory manually and can't use devm:
  81. * this routine may be called from a workqueue, and not from
  82. * the parent .probe.
  83. * If devm_ was used, the memory might never be freed on errors.
  84. */
  85. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  86. if (!ctx)
  87. return NULL;
  88. ctx->count = count;
  89. ctx->link_mask = res->link_mask;
  90. struct resource *sdw_res __free(kfree) = kzalloc(sizeof(*sdw_res),
  91. GFP_KERNEL);
  92. if (!sdw_res) {
  93. kfree(ctx);
  94. return NULL;
  95. }
  96. sdw_res->flags = IORESOURCE_MEM;
  97. sdw_res->start = res->addr;
  98. sdw_res->end = res->addr + res->reg_range;
  99. memset(&pdevinfo, 0, sizeof(pdevinfo));
  100. link_mask = ctx->link_mask;
  101. for (index = 0; index < count; index++) {
  102. if (!(link_mask & BIT(index)))
  103. continue;
  104. sdw_pdata[index].instance = index;
  105. sdw_pdata[index].acp_sdw_lock = res->acp_lock;
  106. pdevinfo[index].name = "amd_sdw_manager";
  107. pdevinfo[index].id = index;
  108. pdevinfo[index].parent = res->parent;
  109. pdevinfo[index].num_res = 1;
  110. pdevinfo[index].res = sdw_res;
  111. pdevinfo[index].data = &sdw_pdata[index];
  112. pdevinfo[index].size_data = sizeof(struct acp_sdw_pdata);
  113. pdevinfo[index].fwnode = acpi_fwnode_handle(adev);
  114. ctx->pdev[index] = platform_device_register_full(&pdevinfo[index]);
  115. if (IS_ERR(ctx->pdev[index]))
  116. goto err;
  117. }
  118. return ctx;
  119. err:
  120. while (index--) {
  121. if (!(link_mask & BIT(index)))
  122. continue;
  123. platform_device_unregister(ctx->pdev[index]);
  124. }
  125. kfree(ctx);
  126. return NULL;
  127. }
  128. static int sdw_amd_startup(struct sdw_amd_ctx *ctx)
  129. {
  130. struct amd_sdw_manager *amd_manager;
  131. int i, ret;
  132. /* Startup SDW Manager devices */
  133. for (i = 0; i < ctx->count; i++) {
  134. if (!(ctx->link_mask & BIT(i)))
  135. continue;
  136. amd_manager = dev_get_drvdata(&ctx->pdev[i]->dev);
  137. ret = amd_sdw_manager_start(amd_manager);
  138. if (ret)
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. int sdw_amd_probe(struct sdw_amd_res *res, struct sdw_amd_ctx **sdw_ctx)
  144. {
  145. *sdw_ctx = sdw_amd_probe_controller(res);
  146. if (!*sdw_ctx)
  147. return -ENODEV;
  148. return sdw_amd_startup(*sdw_ctx);
  149. }
  150. EXPORT_SYMBOL_NS(sdw_amd_probe, SOUNDWIRE_AMD_INIT);
  151. void sdw_amd_exit(struct sdw_amd_ctx *ctx)
  152. {
  153. sdw_amd_cleanup(ctx);
  154. kfree(ctx->ids);
  155. kfree(ctx);
  156. }
  157. EXPORT_SYMBOL_NS(sdw_amd_exit, SOUNDWIRE_AMD_INIT);
  158. int sdw_amd_get_slave_info(struct sdw_amd_ctx *ctx)
  159. {
  160. struct amd_sdw_manager *amd_manager;
  161. struct sdw_bus *bus;
  162. struct sdw_slave *slave;
  163. struct list_head *node;
  164. int index;
  165. int i = 0;
  166. int num_slaves = 0;
  167. for (index = 0; index < ctx->count; index++) {
  168. if (!(ctx->link_mask & BIT(index)))
  169. continue;
  170. amd_manager = dev_get_drvdata(&ctx->pdev[index]->dev);
  171. if (!amd_manager)
  172. return -ENODEV;
  173. bus = &amd_manager->bus;
  174. /* Calculate number of slaves */
  175. list_for_each(node, &bus->slaves)
  176. num_slaves++;
  177. }
  178. ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
  179. if (!ctx->ids)
  180. return -ENOMEM;
  181. ctx->num_slaves = num_slaves;
  182. for (index = 0; index < ctx->count; index++) {
  183. if (!(ctx->link_mask & BIT(index)))
  184. continue;
  185. amd_manager = dev_get_drvdata(&ctx->pdev[index]->dev);
  186. if (amd_manager) {
  187. bus = &amd_manager->bus;
  188. list_for_each_entry(slave, &bus->slaves, node) {
  189. ctx->ids[i].id = slave->id;
  190. ctx->ids[i].link_id = bus->link_id;
  191. i++;
  192. }
  193. }
  194. }
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_NS(sdw_amd_get_slave_info, SOUNDWIRE_AMD_INIT);
  198. MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
  199. MODULE_DESCRIPTION("AMD SoundWire Init Library");
  200. MODULE_LICENSE("Dual BSD/GPL");