msm-dai-q6-hdmi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/bitops.h>
  17. #include <linux/slab.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/soc.h>
  21. #include <sound/apr_audio.h>
  22. #include <sound/q6afe.h>
  23. #include <sound/q6adm.h>
  24. #include <sound/msm-dai-q6.h>
  25. #include <mach/msm_hdmi_audio.h>
  26. enum {
  27. STATUS_PORT_STARTED, /* track if AFE port has started */
  28. STATUS_MAX
  29. };
  30. struct msm_dai_q6_hdmi_dai_data {
  31. DECLARE_BITMAP(status_mask, STATUS_MAX);
  32. u32 rate;
  33. u32 channels;
  34. union afe_port_config port_config;
  35. };
  36. static int msm_dai_q6_hdmi_format_put(struct snd_kcontrol *kcontrol,
  37. struct snd_ctl_elem_value *ucontrol)
  38. {
  39. struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
  40. int value = ucontrol->value.integer.value[0];
  41. dai_data->port_config.hdmi_multi_ch.data_type = value;
  42. pr_debug("%s: value = %d\n", __func__, value);
  43. return 0;
  44. }
  45. static int msm_dai_q6_hdmi_format_get(struct snd_kcontrol *kcontrol,
  46. struct snd_ctl_elem_value *ucontrol)
  47. {
  48. struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
  49. ucontrol->value.integer.value[0] =
  50. dai_data->port_config.hdmi_multi_ch.data_type;
  51. return 0;
  52. }
  53. /* HDMI format field for AFE_PORT_MULTI_CHAN_HDMI_AUDIO_IF_CONFIG command
  54. * 0: linear PCM
  55. * 1: non-linear PCM
  56. */
  57. static const char *hdmi_format[] = {
  58. "LPCM",
  59. "Compr"
  60. };
  61. static const struct soc_enum hdmi_config_enum[] = {
  62. SOC_ENUM_SINGLE_EXT(2, hdmi_format),
  63. };
  64. static const struct snd_kcontrol_new hdmi_config_controls[] = {
  65. SOC_ENUM_EXT("HDMI RX Format", hdmi_config_enum[0],
  66. msm_dai_q6_hdmi_format_get,
  67. msm_dai_q6_hdmi_format_put),
  68. };
  69. /* Current implementation assumes hw_param is called once
  70. * This may not be the case but what to do when ADM and AFE
  71. * port are already opened and parameter changes
  72. */
  73. static int msm_dai_q6_hdmi_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct snd_soc_dai *dai)
  76. {
  77. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  78. u32 channel_allocation = 0;
  79. u32 level_shift = 0; /* 0dB */
  80. bool down_mix = FALSE;
  81. int sample_rate = 48000;
  82. dai_data->channels = params_channels(params);
  83. dai_data->rate = params_rate(params);
  84. dai_data->port_config.hdmi_multi_ch.reserved = 0;
  85. switch (dai_data->rate) {
  86. case 48000:
  87. sample_rate = HDMI_SAMPLE_RATE_48KHZ;
  88. break;
  89. case 44100:
  90. sample_rate = HDMI_SAMPLE_RATE_44_1KHZ;
  91. break;
  92. case 32000:
  93. sample_rate = HDMI_SAMPLE_RATE_32KHZ;
  94. break;
  95. }
  96. hdmi_msm_audio_sample_rate_reset(sample_rate);
  97. switch (dai_data->channels) {
  98. case 2:
  99. channel_allocation = 0;
  100. hdmi_msm_audio_info_setup(1, MSM_HDMI_AUDIO_CHANNEL_2,
  101. channel_allocation, level_shift, down_mix);
  102. dai_data->port_config.hdmi_multi_ch.channel_allocation =
  103. channel_allocation;
  104. break;
  105. case 6:
  106. channel_allocation = 0x0B;
  107. hdmi_msm_audio_info_setup(1, MSM_HDMI_AUDIO_CHANNEL_6,
  108. channel_allocation, level_shift, down_mix);
  109. dai_data->port_config.hdmi_multi_ch.channel_allocation =
  110. channel_allocation;
  111. break;
  112. case 8:
  113. channel_allocation = 0x1F;
  114. hdmi_msm_audio_info_setup(1, MSM_HDMI_AUDIO_CHANNEL_8,
  115. channel_allocation, level_shift, down_mix);
  116. dai_data->port_config.hdmi_multi_ch.channel_allocation =
  117. channel_allocation;
  118. break;
  119. default:
  120. dev_err(dai->dev, "invalid Channels = %u\n",
  121. dai_data->channels);
  122. return -EINVAL;
  123. }
  124. dev_dbg(dai->dev, "%s() num_ch = %u rate =%u"
  125. " channel_allocation = %u data type = %d\n", __func__,
  126. dai_data->channels,
  127. dai_data->rate,
  128. dai_data->port_config.hdmi_multi_ch.channel_allocation,
  129. dai_data->port_config.hdmi_multi_ch.data_type);
  130. return 0;
  131. }
  132. static void msm_dai_q6_hdmi_shutdown(struct snd_pcm_substream *substream,
  133. struct snd_soc_dai *dai)
  134. {
  135. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  136. int rc = 0;
  137. if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  138. pr_info("%s: afe port not started. dai_data->status_mask"
  139. " = %ld\n", __func__, *dai_data->status_mask);
  140. return;
  141. }
  142. rc = afe_close(dai->id); /* can block */
  143. if (IS_ERR_VALUE(rc))
  144. dev_err(dai->dev, "fail to close AFE port\n");
  145. pr_debug("%s: dai_data->status_mask = %ld\n", __func__,
  146. *dai_data->status_mask);
  147. clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
  148. }
  149. static int msm_dai_q6_hdmi_prepare(struct snd_pcm_substream *substream,
  150. struct snd_soc_dai *dai)
  151. {
  152. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  153. int rc = 0;
  154. if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  155. rc = afe_port_start(dai->id, &dai_data->port_config,
  156. dai_data->rate);
  157. if (IS_ERR_VALUE(rc))
  158. dev_err(dai->dev, "fail to open AFE port %x\n",
  159. dai->id);
  160. else
  161. set_bit(STATUS_PORT_STARTED,
  162. dai_data->status_mask);
  163. }
  164. return rc;
  165. }
  166. static int msm_dai_q6_hdmi_dai_probe(struct snd_soc_dai *dai)
  167. {
  168. struct msm_dai_q6_hdmi_dai_data *dai_data;
  169. const struct snd_kcontrol_new *kcontrol;
  170. int rc = 0;
  171. dai_data = kzalloc(sizeof(struct msm_dai_q6_hdmi_dai_data),
  172. GFP_KERNEL);
  173. if (!dai_data) {
  174. dev_err(dai->dev, "DAI-%d: fail to allocate dai data\n",
  175. dai->id);
  176. rc = -ENOMEM;
  177. } else
  178. dev_set_drvdata(dai->dev, dai_data);
  179. kcontrol = &hdmi_config_controls[0];
  180. rc = snd_ctl_add(dai->card->snd_card,
  181. snd_ctl_new1(kcontrol, dai_data));
  182. return rc;
  183. }
  184. static int msm_dai_q6_hdmi_dai_remove(struct snd_soc_dai *dai)
  185. {
  186. struct msm_dai_q6_hdmi_dai_data *dai_data;
  187. int rc;
  188. dai_data = dev_get_drvdata(dai->dev);
  189. /* If AFE port is still up, close it */
  190. if (test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  191. rc = afe_close(dai->id); /* can block */
  192. if (IS_ERR_VALUE(rc))
  193. dev_err(dai->dev, "fail to close AFE port\n");
  194. clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
  195. }
  196. kfree(dai_data);
  197. snd_soc_unregister_dai(dai->dev);
  198. return 0;
  199. }
  200. static struct snd_soc_dai_ops msm_dai_q6_hdmi_ops = {
  201. .prepare = msm_dai_q6_hdmi_prepare,
  202. .hw_params = msm_dai_q6_hdmi_hw_params,
  203. .shutdown = msm_dai_q6_hdmi_shutdown,
  204. };
  205. static struct snd_soc_dai_driver msm_dai_q6_hdmi_hdmi_rx_dai = {
  206. .playback = {
  207. .rates = SNDRV_PCM_RATE_48000,
  208. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  209. .channels_min = 2,
  210. .channels_max = 6,
  211. .rate_max = 48000,
  212. .rate_min = 48000,
  213. },
  214. .ops = &msm_dai_q6_hdmi_ops,
  215. .probe = msm_dai_q6_hdmi_dai_probe,
  216. .remove = msm_dai_q6_hdmi_dai_remove,
  217. };
  218. /* To do: change to register DAIs as batch */
  219. static __devinit int msm_dai_q6_hdmi_dev_probe(struct platform_device *pdev)
  220. {
  221. int rc = 0;
  222. dev_dbg(&pdev->dev, "dev name %s dev-id %d\n",
  223. dev_name(&pdev->dev), pdev->id);
  224. switch (pdev->id) {
  225. case HDMI_RX:
  226. rc = snd_soc_register_dai(&pdev->dev,
  227. &msm_dai_q6_hdmi_hdmi_rx_dai);
  228. break;
  229. default:
  230. dev_err(&pdev->dev, "invalid device ID %d\n", pdev->id);
  231. rc = -ENODEV;
  232. break;
  233. }
  234. return rc;
  235. }
  236. static __devexit int msm_dai_q6_hdmi_dev_remove(struct platform_device *pdev)
  237. {
  238. snd_soc_unregister_dai(&pdev->dev);
  239. return 0;
  240. }
  241. static struct platform_driver msm_dai_q6_hdmi_driver = {
  242. .probe = msm_dai_q6_hdmi_dev_probe,
  243. .remove = msm_dai_q6_hdmi_dev_remove,
  244. .driver = {
  245. .name = "msm-dai-q6-hdmi",
  246. .owner = THIS_MODULE,
  247. },
  248. };
  249. static int __init msm_dai_q6_hdmi_init(void)
  250. {
  251. return platform_driver_register(&msm_dai_q6_hdmi_driver);
  252. }
  253. module_init(msm_dai_q6_hdmi_init);
  254. static void __exit msm_dai_q6_hdmi_exit(void)
  255. {
  256. platform_driver_unregister(&msm_dai_q6_hdmi_driver);
  257. }
  258. module_exit(msm_dai_q6_hdmi_exit);
  259. /* Module information */
  260. MODULE_DESCRIPTION("MSM DSP HDMI DAI driver");
  261. MODULE_LICENSE("GPL v2");