fsl_utils.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Freescale ALSA SoC Machine driver utility
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2010 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <sound/soc.h>
  15. #include "fsl_utils.h"
  16. /**
  17. * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
  18. *
  19. * @ssi_np: pointer to the SSI device tree node
  20. * @name: name of the phandle pointing to the dma channel
  21. * @dai: ASoC DAI link pointer to be filled with platform_name
  22. * @dma_channel_id: dma channel id to be returned
  23. * @dma_id: dma id to be returned
  24. *
  25. * This function determines the dma and channel id for given SSI node. It
  26. * also discovers the platform_name for the ASoC DAI link.
  27. */
  28. int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
  29. const char *name,
  30. struct snd_soc_dai_link *dai,
  31. unsigned int *dma_channel_id,
  32. unsigned int *dma_id)
  33. {
  34. struct resource res;
  35. struct device_node *dma_channel_np, *dma_np;
  36. const u32 *iprop;
  37. int ret;
  38. dma_channel_np = of_parse_phandle(ssi_np, name, 0);
  39. if (!dma_channel_np)
  40. return -EINVAL;
  41. if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
  42. of_node_put(dma_channel_np);
  43. return -EINVAL;
  44. }
  45. /* Determine the dev_name for the device_node. This code mimics the
  46. * behavior of of_device_make_bus_id(). We need this because ASoC uses
  47. * the dev_name() of the device to match the platform (DMA) device with
  48. * the CPU (SSI) device. It's all ugly and hackish, but it works (for
  49. * now).
  50. *
  51. * dai->platform name should already point to an allocated buffer.
  52. */
  53. ret = of_address_to_resource(dma_channel_np, 0, &res);
  54. if (ret) {
  55. of_node_put(dma_channel_np);
  56. return ret;
  57. }
  58. snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
  59. (unsigned long long) res.start, dma_channel_np->name);
  60. iprop = of_get_property(dma_channel_np, "cell-index", NULL);
  61. if (!iprop) {
  62. of_node_put(dma_channel_np);
  63. return -EINVAL;
  64. }
  65. *dma_channel_id = be32_to_cpup(iprop);
  66. dma_np = of_get_parent(dma_channel_np);
  67. iprop = of_get_property(dma_np, "cell-index", NULL);
  68. if (!iprop) {
  69. of_node_put(dma_np);
  70. return -EINVAL;
  71. }
  72. *dma_id = be32_to_cpup(iprop);
  73. of_node_put(dma_np);
  74. of_node_put(dma_channel_np);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
  78. /**
  79. * fsl_asoc_xlate_tdm_slot_mask - generate TDM slot TX/RX mask.
  80. *
  81. * @slots: Number of slots in use.
  82. * @tx_mask: bitmask representing active TX slots.
  83. * @rx_mask: bitmask representing active RX slots.
  84. *
  85. * This function used to generate the TDM slot TX/RX mask. And the TX/RX
  86. * mask will use a 0 bit for an active slot as default, and the default
  87. * active bits are at the LSB of the mask value.
  88. */
  89. int fsl_asoc_xlate_tdm_slot_mask(unsigned int slots,
  90. unsigned int *tx_mask,
  91. unsigned int *rx_mask)
  92. {
  93. if (!slots)
  94. return -EINVAL;
  95. if (tx_mask)
  96. *tx_mask = ~((1 << slots) - 1);
  97. if (rx_mask)
  98. *rx_mask = ~((1 << slots) - 1);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(fsl_asoc_xlate_tdm_slot_mask);
  102. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  103. MODULE_DESCRIPTION("Freescale ASoC utility code");
  104. MODULE_LICENSE("GPL v2");