sti_uniperif.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2015
  3. * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
  4. * for STMicroelectronics.
  5. * License terms: GNU General Public License (GPL), version 2
  6. */
  7. #include <linux/module.h>
  8. #include <linux/pinctrl/consumer.h>
  9. #include "uniperif.h"
  10. /*
  11. * sti_uniperiph_dai_create_ctrl
  12. * This function is used to create Ctrl associated to DAI but also pcm device.
  13. * Request is done by front end to associate ctrl with pcm device id
  14. */
  15. static int sti_uniperiph_dai_create_ctrl(struct snd_soc_dai *dai)
  16. {
  17. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  18. struct uniperif *uni = priv->dai_data.uni;
  19. struct snd_kcontrol_new *ctrl;
  20. int i;
  21. if (!uni->num_ctrls)
  22. return 0;
  23. for (i = 0; i < uni->num_ctrls; i++) {
  24. /*
  25. * Several Control can have same name. Controls are indexed on
  26. * Uniperipheral instance ID
  27. */
  28. ctrl = &uni->snd_ctrls[i];
  29. ctrl->index = uni->info->id;
  30. ctrl->device = uni->info->id;
  31. }
  32. return snd_soc_add_dai_controls(dai, uni->snd_ctrls, uni->num_ctrls);
  33. }
  34. /*
  35. * DAI
  36. */
  37. int sti_uniperiph_dai_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params,
  39. struct snd_soc_dai *dai)
  40. {
  41. struct snd_dmaengine_dai_dma_data *dma_data;
  42. int transfer_size;
  43. transfer_size = params_channels(params) * UNIPERIF_FIFO_FRAMES;
  44. dma_data = snd_soc_dai_get_dma_data(dai, substream);
  45. dma_data->maxburst = transfer_size;
  46. return 0;
  47. }
  48. int sti_uniperiph_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  49. {
  50. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  51. priv->dai_data.uni->daifmt = fmt;
  52. return 0;
  53. }
  54. static int sti_uniperiph_dai_suspend(struct snd_soc_dai *dai)
  55. {
  56. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  57. struct uniperif *uni = priv->dai_data.uni;
  58. int ret;
  59. /* The uniperipheral should be in stopped state */
  60. if (uni->state != UNIPERIF_STATE_STOPPED) {
  61. dev_err(uni->dev, "%s: invalid uni state( %d)",
  62. __func__, (int)uni->state);
  63. return -EBUSY;
  64. }
  65. /* Pinctrl: switch pinstate to sleep */
  66. ret = pinctrl_pm_select_sleep_state(uni->dev);
  67. if (ret)
  68. dev_err(uni->dev, "%s: failed to select pinctrl state",
  69. __func__);
  70. return ret;
  71. }
  72. static int sti_uniperiph_dai_resume(struct snd_soc_dai *dai)
  73. {
  74. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  75. struct uniperif *uni = priv->dai_data.uni;
  76. int ret;
  77. if (of_device_is_compatible(dai->dev->of_node, "st,sti-uni-player")) {
  78. ret = uni_player_resume(uni);
  79. if (ret)
  80. return ret;
  81. }
  82. /* pinctrl: switch pinstate to default */
  83. ret = pinctrl_pm_select_default_state(uni->dev);
  84. if (ret)
  85. dev_err(uni->dev, "%s: failed to select pinctrl state",
  86. __func__);
  87. return ret;
  88. }
  89. static int sti_uniperiph_dai_probe(struct snd_soc_dai *dai)
  90. {
  91. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  92. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  93. /* DMA settings*/
  94. if (of_device_is_compatible(dai->dev->of_node, "st,sti-uni-player"))
  95. snd_soc_dai_init_dma_data(dai, &dai_data->dma_data, NULL);
  96. else
  97. snd_soc_dai_init_dma_data(dai, NULL, &dai_data->dma_data);
  98. dai_data->dma_data.addr = dai_data->uni->fifo_phys_address;
  99. dai_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  100. return sti_uniperiph_dai_create_ctrl(dai);
  101. }
  102. static const struct snd_soc_dai_driver sti_uniperiph_dai_template = {
  103. .probe = sti_uniperiph_dai_probe,
  104. .suspend = sti_uniperiph_dai_suspend,
  105. .resume = sti_uniperiph_dai_resume
  106. };
  107. static const struct snd_soc_component_driver sti_uniperiph_dai_component = {
  108. .name = "sti_cpu_dai",
  109. };
  110. static int sti_uniperiph_cpu_dai_of(struct device_node *node,
  111. struct sti_uniperiph_data *priv)
  112. {
  113. const char *str;
  114. int ret;
  115. struct device *dev = &priv->pdev->dev;
  116. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  117. struct snd_soc_dai_driver *dai = priv->dai;
  118. struct snd_soc_pcm_stream *stream;
  119. struct uniperif *uni;
  120. uni = devm_kzalloc(dev, sizeof(*uni), GFP_KERNEL);
  121. if (!uni)
  122. return -ENOMEM;
  123. *dai = sti_uniperiph_dai_template;
  124. ret = of_property_read_string(node, "dai-name", &str);
  125. if (ret < 0) {
  126. dev_err(dev, "%s: dai name missing.\n", __func__);
  127. return -EINVAL;
  128. }
  129. dai->name = str;
  130. /* Get resources */
  131. uni->mem_region = platform_get_resource(priv->pdev, IORESOURCE_MEM, 0);
  132. if (!uni->mem_region) {
  133. dev_err(dev, "Failed to get memory resource");
  134. return -ENODEV;
  135. }
  136. uni->base = devm_ioremap_resource(dev, uni->mem_region);
  137. if (IS_ERR(uni->base))
  138. return PTR_ERR(uni->base);
  139. uni->fifo_phys_address = uni->mem_region->start +
  140. UNIPERIF_FIFO_DATA_OFFSET(uni);
  141. uni->irq = platform_get_irq(priv->pdev, 0);
  142. if (uni->irq < 0) {
  143. dev_err(dev, "Failed to get IRQ resource");
  144. return -ENXIO;
  145. }
  146. dai_data->uni = uni;
  147. if (of_device_is_compatible(node, "st,sti-uni-player")) {
  148. uni_player_init(priv->pdev, uni);
  149. stream = &dai->playback;
  150. } else {
  151. uni_reader_init(priv->pdev, uni);
  152. stream = &dai->capture;
  153. }
  154. dai->ops = uni->dai_ops;
  155. stream->stream_name = dai->name;
  156. stream->channels_min = uni->hw->channels_min;
  157. stream->channels_max = uni->hw->channels_max;
  158. stream->rates = uni->hw->rates;
  159. stream->formats = uni->hw->formats;
  160. return 0;
  161. }
  162. static const struct snd_dmaengine_pcm_config dmaengine_pcm_config = {
  163. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  164. };
  165. static int sti_uniperiph_probe(struct platform_device *pdev)
  166. {
  167. struct sti_uniperiph_data *priv;
  168. struct device_node *node = pdev->dev.of_node;
  169. int ret;
  170. /* Allocate the private data and the CPU_DAI array */
  171. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  172. if (!priv)
  173. return -ENOMEM;
  174. priv->dai = devm_kzalloc(&pdev->dev, sizeof(*priv->dai), GFP_KERNEL);
  175. if (!priv->dai)
  176. return -ENOMEM;
  177. priv->pdev = pdev;
  178. ret = sti_uniperiph_cpu_dai_of(node, priv);
  179. dev_set_drvdata(&pdev->dev, priv);
  180. ret = devm_snd_soc_register_component(&pdev->dev,
  181. &sti_uniperiph_dai_component,
  182. priv->dai, 1);
  183. if (ret < 0)
  184. return ret;
  185. return devm_snd_dmaengine_pcm_register(&pdev->dev,
  186. &dmaengine_pcm_config, 0);
  187. }
  188. static const struct of_device_id snd_soc_sti_match[] = {
  189. { .compatible = "st,sti-uni-player", },
  190. { .compatible = "st,sti-uni-reader", },
  191. {},
  192. };
  193. static struct platform_driver sti_uniperiph_driver = {
  194. .driver = {
  195. .name = "sti-uniperiph-dai",
  196. .of_match_table = snd_soc_sti_match,
  197. },
  198. .probe = sti_uniperiph_probe,
  199. };
  200. module_platform_driver(sti_uniperiph_driver);
  201. MODULE_DESCRIPTION("uniperipheral DAI driver");
  202. MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
  203. MODULE_LICENSE("GPL v2");