intel_init.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/export.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/auxiliary_bus.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/soundwire/sdw_intel.h>
  16. #include "cadence_master.h"
  17. #include "bus.h"
  18. #include "intel.h"
  19. #include "intel_auxdevice.h"
  20. static void intel_link_dev_release(struct device *dev)
  21. {
  22. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  23. struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
  24. kfree(ldev);
  25. }
  26. /* alloc, init and add link devices */
  27. static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
  28. struct sdw_intel_ctx *ctx,
  29. struct fwnode_handle *fwnode,
  30. const char *name,
  31. int link_id)
  32. {
  33. struct sdw_intel_link_dev *ldev;
  34. struct sdw_intel_link_res *link;
  35. struct auxiliary_device *auxdev;
  36. int ret;
  37. ldev = kzalloc_obj(*ldev);
  38. if (!ldev)
  39. return ERR_PTR(-ENOMEM);
  40. auxdev = &ldev->auxdev;
  41. auxdev->name = name;
  42. auxdev->dev.parent = res->parent;
  43. auxdev->dev.fwnode = fwnode;
  44. auxdev->dev.release = intel_link_dev_release;
  45. /* we don't use an IDA since we already have a link ID */
  46. auxdev->id = link_id;
  47. /*
  48. * keep a handle on the allocated memory, to be used in all other functions.
  49. * Since the same pattern is used to skip links that are not enabled, there is
  50. * no need to check if ctx->ldev[i] is NULL later on.
  51. */
  52. ctx->ldev[link_id] = ldev;
  53. /* Add link information used in the driver probe */
  54. link = &ldev->link_res;
  55. link->hw_ops = res->hw_ops;
  56. link->mmio_base = res->mmio_base;
  57. if (!res->ext) {
  58. link->registers = res->mmio_base + SDW_LINK_BASE
  59. + (SDW_LINK_SIZE * link_id);
  60. link->ip_offset = 0;
  61. link->shim = res->mmio_base + res->shim_base;
  62. link->alh = res->mmio_base + res->alh_base;
  63. link->shim_lock = &ctx->shim_lock;
  64. } else {
  65. link->registers = res->mmio_base + SDW_IP_BASE(link_id);
  66. link->ip_offset = SDW_CADENCE_MCP_IP_OFFSET;
  67. link->shim = res->mmio_base + SDW_SHIM2_GENERIC_BASE(link_id);
  68. link->shim_vs = res->mmio_base + SDW_SHIM2_VS_BASE(link_id);
  69. link->shim_lock = res->eml_lock;
  70. link->mic_privacy = res->mic_privacy;
  71. }
  72. link->ops = res->ops;
  73. link->dev = res->dev;
  74. link->clock_stop_quirks = res->clock_stop_quirks;
  75. link->shim_mask = &ctx->shim_mask;
  76. link->link_mask = ctx->link_mask;
  77. link->hbus = res->hbus;
  78. /* now follow the two-step init/add sequence */
  79. ret = auxiliary_device_init(auxdev);
  80. if (ret < 0) {
  81. dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
  82. name, link_id);
  83. kfree(ldev);
  84. return ERR_PTR(ret);
  85. }
  86. ret = auxiliary_device_add(&ldev->auxdev);
  87. if (ret < 0) {
  88. dev_err(res->parent, "failed to add link dev %s link_id %d\n",
  89. ldev->auxdev.name, link_id);
  90. /* ldev will be freed with the put_device() and .release sequence */
  91. auxiliary_device_uninit(&ldev->auxdev);
  92. return ERR_PTR(ret);
  93. }
  94. return ldev;
  95. }
  96. static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
  97. {
  98. auxiliary_device_delete(&ldev->auxdev);
  99. auxiliary_device_uninit(&ldev->auxdev);
  100. }
  101. static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
  102. {
  103. struct sdw_intel_link_dev *ldev;
  104. u32 link_mask;
  105. int i;
  106. link_mask = ctx->link_mask;
  107. for (i = 0; i < ctx->count; i++) {
  108. if (!(link_mask & BIT(i)))
  109. continue;
  110. ldev = ctx->ldev[i];
  111. pm_runtime_disable(&ldev->auxdev.dev);
  112. if (!ldev->link_res.clock_stop_quirks)
  113. pm_runtime_put_noidle(ldev->link_res.dev);
  114. intel_link_dev_unregister(ldev);
  115. }
  116. return 0;
  117. }
  118. irqreturn_t sdw_intel_thread(int irq, void *dev_id)
  119. {
  120. struct sdw_intel_ctx *ctx = dev_id;
  121. struct sdw_intel_link_res *link;
  122. list_for_each_entry(link, &ctx->link_list, list)
  123. sdw_cdns_irq(irq, link->cdns);
  124. return IRQ_HANDLED;
  125. }
  126. EXPORT_SYMBOL_NS(sdw_intel_thread, "SOUNDWIRE_INTEL_INIT");
  127. static struct sdw_intel_ctx
  128. *sdw_intel_probe_controller(struct sdw_intel_res *res)
  129. {
  130. struct sdw_intel_link_res *link;
  131. struct sdw_intel_link_dev *ldev;
  132. struct sdw_intel_ctx *ctx;
  133. struct acpi_device *adev;
  134. struct sdw_slave *slave;
  135. struct list_head *node;
  136. struct sdw_bus *bus;
  137. u32 link_mask;
  138. int num_slaves = 0;
  139. int count;
  140. int i;
  141. if (!res)
  142. return NULL;
  143. adev = acpi_fetch_acpi_dev(res->handle);
  144. if (!adev)
  145. return NULL;
  146. if (!res->count)
  147. return NULL;
  148. count = res->count;
  149. dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
  150. /*
  151. * we need to alloc/free memory manually and can't use devm:
  152. * this routine may be called from a workqueue, and not from
  153. * the parent .probe.
  154. * If devm_ was used, the memory might never be freed on errors.
  155. */
  156. ctx = kzalloc_obj(*ctx);
  157. if (!ctx)
  158. return NULL;
  159. ctx->count = count;
  160. /*
  161. * allocate the array of pointers. The link-specific data is allocated
  162. * as part of the first loop below and released with the auxiliary_device_uninit().
  163. * If some links are disabled, the link pointer will remain NULL. Given that the
  164. * number of links is small, this is simpler than using a list to keep track of links.
  165. */
  166. ctx->ldev = kzalloc_objs(*ctx->ldev, ctx->count);
  167. if (!ctx->ldev) {
  168. kfree(ctx);
  169. return NULL;
  170. }
  171. ctx->mmio_base = res->mmio_base;
  172. ctx->shim_base = res->shim_base;
  173. ctx->alh_base = res->alh_base;
  174. ctx->link_mask = res->link_mask;
  175. ctx->handle = res->handle;
  176. mutex_init(&ctx->shim_lock);
  177. link_mask = ctx->link_mask;
  178. INIT_LIST_HEAD(&ctx->link_list);
  179. for (i = 0; i < count; i++) {
  180. if (!(link_mask & BIT(i)))
  181. continue;
  182. /*
  183. * init and add a device for each link
  184. *
  185. * The name of the device will be soundwire_intel.link.[i],
  186. * with the "soundwire_intel" module prefix automatically added
  187. * by the auxiliary bus core.
  188. */
  189. ldev = intel_link_dev_register(res,
  190. ctx,
  191. acpi_fwnode_handle(adev),
  192. "link",
  193. i);
  194. if (IS_ERR(ldev))
  195. goto err;
  196. link = &ldev->link_res;
  197. link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
  198. if (!link->cdns) {
  199. dev_err(&adev->dev, "failed to get link->cdns\n");
  200. /*
  201. * 1 will be subtracted from i in the err label, but we need to call
  202. * intel_link_dev_unregister for this ldev, so plus 1 now
  203. */
  204. i++;
  205. goto err;
  206. }
  207. list_add_tail(&link->list, &ctx->link_list);
  208. bus = &link->cdns->bus;
  209. /* Calculate number of slaves */
  210. list_for_each(node, &bus->slaves)
  211. num_slaves++;
  212. }
  213. ctx->peripherals = kmalloc_flex(*ctx->peripherals, array, num_slaves);
  214. if (!ctx->peripherals)
  215. goto err;
  216. ctx->peripherals->num_peripherals = num_slaves;
  217. i = 0;
  218. list_for_each_entry(link, &ctx->link_list, list) {
  219. bus = &link->cdns->bus;
  220. list_for_each_entry(slave, &bus->slaves, node) {
  221. ctx->peripherals->array[i] = slave;
  222. i++;
  223. }
  224. }
  225. return ctx;
  226. err:
  227. while (i--) {
  228. if (!(link_mask & BIT(i)))
  229. continue;
  230. ldev = ctx->ldev[i];
  231. intel_link_dev_unregister(ldev);
  232. }
  233. kfree(ctx->ldev);
  234. kfree(ctx);
  235. return NULL;
  236. }
  237. static int
  238. sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
  239. {
  240. struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
  241. struct sdw_intel_link_dev *ldev;
  242. u32 link_mask;
  243. int i;
  244. if (!adev)
  245. return -EINVAL;
  246. if (!ctx->ldev)
  247. return -EINVAL;
  248. link_mask = ctx->link_mask;
  249. /* Startup SDW Master devices */
  250. for (i = 0; i < ctx->count; i++) {
  251. if (!(link_mask & BIT(i)))
  252. continue;
  253. ldev = ctx->ldev[i];
  254. intel_link_startup(&ldev->auxdev);
  255. if (!ldev->link_res.clock_stop_quirks) {
  256. /*
  257. * we need to prevent the parent PCI device
  258. * from entering pm_runtime suspend, so that
  259. * power rails to the SoundWire IP are not
  260. * turned off.
  261. */
  262. pm_runtime_get_noresume(ldev->link_res.dev);
  263. }
  264. }
  265. return 0;
  266. }
  267. /**
  268. * sdw_intel_probe() - SoundWire Intel probe routine
  269. * @res: resource data
  270. *
  271. * This registers an auxiliary device for each Master handled by the controller,
  272. * and SoundWire Master and Slave devices will be created by the auxiliary
  273. * device probe. All the information necessary is stored in the context, and
  274. * the res argument pointer can be freed after this step.
  275. * This function will be called after sdw_intel_acpi_scan() by SOF probe.
  276. */
  277. struct sdw_intel_ctx
  278. *sdw_intel_probe(struct sdw_intel_res *res)
  279. {
  280. return sdw_intel_probe_controller(res);
  281. }
  282. EXPORT_SYMBOL_NS(sdw_intel_probe, "SOUNDWIRE_INTEL_INIT");
  283. /**
  284. * sdw_intel_startup() - SoundWire Intel startup
  285. * @ctx: SoundWire context allocated in the probe
  286. *
  287. * Startup Intel SoundWire controller. This function will be called after
  288. * Intel Audio DSP is powered up.
  289. */
  290. int sdw_intel_startup(struct sdw_intel_ctx *ctx)
  291. {
  292. return sdw_intel_startup_controller(ctx);
  293. }
  294. EXPORT_SYMBOL_NS(sdw_intel_startup, "SOUNDWIRE_INTEL_INIT");
  295. /**
  296. * sdw_intel_exit() - SoundWire Intel exit
  297. * @ctx: SoundWire context allocated in the probe
  298. *
  299. * Delete the controller instances created and cleanup
  300. */
  301. void sdw_intel_exit(struct sdw_intel_ctx *ctx)
  302. {
  303. struct sdw_intel_link_res *link;
  304. /* we first resume links and devices and wait synchronously before the cleanup */
  305. list_for_each_entry(link, &ctx->link_list, list) {
  306. struct sdw_bus *bus = &link->cdns->bus;
  307. int ret;
  308. ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
  309. if (ret < 0)
  310. dev_err(bus->dev, "%s: intel_resume_child_device failed: %d\n",
  311. __func__, ret);
  312. }
  313. sdw_intel_cleanup(ctx);
  314. kfree(ctx->peripherals);
  315. kfree(ctx->ldev);
  316. kfree(ctx);
  317. }
  318. EXPORT_SYMBOL_NS(sdw_intel_exit, "SOUNDWIRE_INTEL_INIT");
  319. void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
  320. {
  321. struct sdw_intel_link_dev *ldev;
  322. u32 link_mask;
  323. int i;
  324. if (!ctx->ldev)
  325. return;
  326. link_mask = ctx->link_mask;
  327. /* Startup SDW Master devices */
  328. for (i = 0; i < ctx->count; i++) {
  329. if (!(link_mask & BIT(i)))
  330. continue;
  331. ldev = ctx->ldev[i];
  332. intel_link_process_wakeen_event(&ldev->auxdev);
  333. }
  334. }
  335. EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, "SOUNDWIRE_INTEL_INIT");
  336. MODULE_LICENSE("Dual BSD/GPL");
  337. MODULE_DESCRIPTION("Intel Soundwire Init Library");