intel_init.c 10.0 KB

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