fsl_utils.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale ALSA SoC Machine driver utility
  4. //
  5. // Author: Timur Tabi <timur@freescale.com>
  6. //
  7. // Copyright 2010 Freescale Semiconductor, Inc.
  8. #include <linux/module.h>
  9. #include <linux/of_address.h>
  10. #include <sound/soc.h>
  11. #include "fsl_utils.h"
  12. /**
  13. * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
  14. *
  15. * @ssi_np: pointer to the SSI device tree node
  16. * @name: name of the phandle pointing to the dma channel
  17. * @dai: ASoC DAI link pointer to be filled with platform_name
  18. * @dma_channel_id: dma channel id to be returned
  19. * @dma_id: dma id to be returned
  20. *
  21. * This function determines the dma and channel id for given SSI node. It
  22. * also discovers the platform_name for the ASoC DAI link.
  23. */
  24. int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
  25. const char *name,
  26. struct snd_soc_dai_link *dai,
  27. unsigned int *dma_channel_id,
  28. unsigned int *dma_id)
  29. {
  30. struct resource res;
  31. struct device_node *dma_channel_np, *dma_np;
  32. const __be32 *iprop;
  33. int ret;
  34. dma_channel_np = of_parse_phandle(ssi_np, name, 0);
  35. if (!dma_channel_np)
  36. return -EINVAL;
  37. if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
  38. of_node_put(dma_channel_np);
  39. return -EINVAL;
  40. }
  41. /* Determine the dev_name for the device_node. This code mimics the
  42. * behavior of of_device_make_bus_id(). We need this because ASoC uses
  43. * the dev_name() of the device to match the platform (DMA) device with
  44. * the CPU (SSI) device. It's all ugly and hackish, but it works (for
  45. * now).
  46. *
  47. * dai->platform name should already point to an allocated buffer.
  48. */
  49. ret = of_address_to_resource(dma_channel_np, 0, &res);
  50. if (ret) {
  51. of_node_put(dma_channel_np);
  52. return ret;
  53. }
  54. snprintf((char *)dai->platforms->name, DAI_NAME_SIZE, "%llx.%pOFn",
  55. (unsigned long long) res.start, dma_channel_np);
  56. iprop = of_get_property(dma_channel_np, "cell-index", NULL);
  57. if (!iprop) {
  58. of_node_put(dma_channel_np);
  59. return -EINVAL;
  60. }
  61. *dma_channel_id = be32_to_cpup(iprop);
  62. dma_np = of_get_parent(dma_channel_np);
  63. iprop = of_get_property(dma_np, "cell-index", NULL);
  64. if (!iprop) {
  65. of_node_put(dma_np);
  66. of_node_put(dma_channel_np);
  67. return -EINVAL;
  68. }
  69. *dma_id = be32_to_cpup(iprop);
  70. of_node_put(dma_np);
  71. of_node_put(dma_channel_np);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
  75. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  76. MODULE_DESCRIPTION("Freescale ASoC utility code");
  77. MODULE_LICENSE("GPL v2");