fsl_utils.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <linux/of_i2c.h>
  15. #include <sound/soc.h>
  16. #include "fsl_utils.h"
  17. /**
  18. * fsl_asoc_get_codec_dev_name - determine the dev_name for a codec node
  19. *
  20. * @np: pointer to the I2C device tree node
  21. * @buf: buffer to be filled with the dev_name of the I2C device
  22. * @len: the length of the buffer
  23. *
  24. * This function determines the dev_name for an I2C node. This is the name
  25. * that would be returned by dev_name() if this device_node were part of a
  26. * 'struct device' It's ugly and hackish, but it works.
  27. *
  28. * The dev_name for such devices include the bus number and I2C address. For
  29. * example, "cs4270.0-004f".
  30. */
  31. int fsl_asoc_get_codec_dev_name(struct device_node *np, char *buf, size_t len)
  32. {
  33. const u32 *iprop;
  34. u32 addr;
  35. char temp[DAI_NAME_SIZE];
  36. struct i2c_client *i2c;
  37. of_modalias_node(np, temp, DAI_NAME_SIZE);
  38. iprop = of_get_property(np, "reg", NULL);
  39. if (!iprop)
  40. return -EINVAL;
  41. addr = be32_to_cpup(iprop);
  42. /* We need the adapter number */
  43. i2c = of_find_i2c_device_by_node(np);
  44. if (!i2c) {
  45. put_device(&i2c->dev);
  46. return -ENODEV;
  47. }
  48. snprintf(buf, len, "%s.%u-%04x", temp, i2c->adapter->nr, addr);
  49. put_device(&i2c->dev);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(fsl_asoc_get_codec_dev_name);
  53. /**
  54. * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
  55. *
  56. * @ssi_np: pointer to the SSI device tree node
  57. * @name: name of the phandle pointing to the dma channel
  58. * @dai: ASoC DAI link pointer to be filled with platform_name
  59. * @dma_channel_id: dma channel id to be returned
  60. * @dma_id: dma id to be returned
  61. *
  62. * This function determines the dma and channel id for given SSI node. It
  63. * also discovers the platform_name for the ASoC DAI link.
  64. */
  65. int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
  66. const char *name,
  67. struct snd_soc_dai_link *dai,
  68. unsigned int *dma_channel_id,
  69. unsigned int *dma_id)
  70. {
  71. struct resource res;
  72. struct device_node *dma_channel_np, *dma_np;
  73. const u32 *iprop;
  74. int ret;
  75. dma_channel_np = of_parse_phandle(ssi_np, name, 0);
  76. if (!dma_channel_np)
  77. return -EINVAL;
  78. if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
  79. of_node_put(dma_channel_np);
  80. return -EINVAL;
  81. }
  82. /* Determine the dev_name for the device_node. This code mimics the
  83. * behavior of of_device_make_bus_id(). We need this because ASoC uses
  84. * the dev_name() of the device to match the platform (DMA) device with
  85. * the CPU (SSI) device. It's all ugly and hackish, but it works (for
  86. * now).
  87. *
  88. * dai->platform name should already point to an allocated buffer.
  89. */
  90. ret = of_address_to_resource(dma_channel_np, 0, &res);
  91. if (ret) {
  92. of_node_put(dma_channel_np);
  93. return ret;
  94. }
  95. snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
  96. (unsigned long long) res.start, dma_channel_np->name);
  97. iprop = of_get_property(dma_channel_np, "cell-index", NULL);
  98. if (!iprop) {
  99. of_node_put(dma_channel_np);
  100. return -EINVAL;
  101. }
  102. *dma_channel_id = be32_to_cpup(iprop);
  103. dma_np = of_get_parent(dma_channel_np);
  104. iprop = of_get_property(dma_np, "cell-index", NULL);
  105. if (!iprop) {
  106. of_node_put(dma_np);
  107. return -EINVAL;
  108. }
  109. *dma_id = be32_to_cpup(iprop);
  110. of_node_put(dma_np);
  111. of_node_put(dma_channel_np);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
  115. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  116. MODULE_DESCRIPTION("Freescale ASoC utility code");
  117. MODULE_LICENSE("GPL v2");