msm-dai-q6-hdmi-v2.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Copyright (c) 2012-2013, 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 <linux/of_device.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/soc.h>
  22. #include <sound/apr_audio-v2.h>
  23. #include <sound/q6afe-v2.h>
  24. #include <sound/msm-dai-q6-v2.h>
  25. #include <sound/pcm_params.h>
  26. #define HDMI_RX_CA_MAX 0x32
  27. enum {
  28. STATUS_PORT_STARTED, /* track if AFE port has started */
  29. STATUS_MAX
  30. };
  31. struct msm_hdmi_ca {
  32. bool set_ca;
  33. u32 ca;
  34. };
  35. static struct msm_hdmi_ca hdmi_ca = { false, 0x0 };
  36. struct msm_dai_q6_hdmi_dai_data {
  37. DECLARE_BITMAP(status_mask, STATUS_MAX);
  38. u32 rate;
  39. u32 channels;
  40. union afe_port_config port_config;
  41. };
  42. static int msm_dai_q6_hdmi_format_put(struct snd_kcontrol *kcontrol,
  43. struct snd_ctl_elem_value *ucontrol)
  44. {
  45. struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
  46. int value = ucontrol->value.integer.value[0];
  47. dai_data->port_config.hdmi_multi_ch.datatype = value;
  48. pr_debug("%s: value = %d\n", __func__, value);
  49. return 0;
  50. }
  51. static int msm_dai_q6_hdmi_format_get(struct snd_kcontrol *kcontrol,
  52. struct snd_ctl_elem_value *ucontrol)
  53. {
  54. struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
  55. ucontrol->value.integer.value[0] =
  56. dai_data->port_config.hdmi_multi_ch.datatype;
  57. return 0;
  58. }
  59. static int msm_dai_q6_hdmi_ca_put(struct snd_kcontrol *kcontrol,
  60. struct snd_ctl_elem_value *ucontrol)
  61. {
  62. hdmi_ca.ca = ucontrol->value.integer.value[0];
  63. hdmi_ca.set_ca = true;
  64. return 0;
  65. }
  66. static int msm_dai_q6_hdmi_ca_get(struct snd_kcontrol *kcontrol,
  67. struct snd_ctl_elem_value *ucontrol)
  68. {
  69. ucontrol->value.integer.value[0] = hdmi_ca.ca;
  70. return 0;
  71. }
  72. /* HDMI format field for AFE_PORT_MULTI_CHAN_HDMI_AUDIO_IF_CONFIG command
  73. * 0: linear PCM
  74. * 1: non-linear PCM
  75. */
  76. static const char * const hdmi_format[] = {
  77. "LPCM",
  78. "Compr"
  79. };
  80. static const struct soc_enum hdmi_config_enum[] = {
  81. SOC_ENUM_SINGLE_EXT(2, hdmi_format),
  82. };
  83. static const struct snd_kcontrol_new hdmi_config_controls[] = {
  84. SOC_ENUM_EXT("HDMI RX Format", hdmi_config_enum[0],
  85. msm_dai_q6_hdmi_format_get,
  86. msm_dai_q6_hdmi_format_put),
  87. SOC_SINGLE_MULTI_EXT("HDMI RX CA", SND_SOC_NOPM, 0,
  88. HDMI_RX_CA_MAX, 0, 1,
  89. msm_dai_q6_hdmi_ca_get,
  90. msm_dai_q6_hdmi_ca_put),
  91. };
  92. /* Current implementation assumes hw_param is called once
  93. * This may not be the case but what to do when ADM and AFE
  94. * port are already opened and parameter changes
  95. */
  96. static int msm_dai_q6_hdmi_hw_params(struct snd_pcm_substream *substream,
  97. struct snd_pcm_hw_params *params,
  98. struct snd_soc_dai *dai)
  99. {
  100. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  101. dai_data->channels = params_channels(params);
  102. dai_data->rate = params_rate(params);
  103. dai_data->port_config.hdmi_multi_ch.reserved = 0;
  104. dai_data->port_config.hdmi_multi_ch.hdmi_cfg_minor_version = 1;
  105. dai_data->port_config.hdmi_multi_ch.sample_rate = dai_data->rate;
  106. switch (params_format(params)) {
  107. case SNDRV_PCM_FORMAT_S16_LE:
  108. dai_data->port_config.hdmi_multi_ch.bit_width = 16;
  109. break;
  110. case SNDRV_PCM_FORMAT_S24_LE:
  111. dai_data->port_config.hdmi_multi_ch.bit_width = 24;
  112. break;
  113. }
  114. /*refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4*/
  115. switch (dai_data->channels) {
  116. case 2:
  117. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0;
  118. break;
  119. case 3:
  120. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x02;
  121. break;
  122. case 4:
  123. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x06;
  124. break;
  125. case 5:
  126. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0A;
  127. break;
  128. case 6:
  129. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0B;
  130. break;
  131. case 7:
  132. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x12;
  133. break;
  134. case 8:
  135. dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x13;
  136. break;
  137. default:
  138. dev_err(dai->dev, "invalid Channels = %u\n",
  139. dai_data->channels);
  140. return -EINVAL;
  141. }
  142. dev_dbg(dai->dev, "%s() minor version: %u samplerate: %u bitwidth: %u\n"
  143. "num_ch = %u channel_allocation = %u datatype = %d\n", __func__,
  144. dai_data->port_config.hdmi_multi_ch.hdmi_cfg_minor_version,
  145. dai_data->port_config.hdmi_multi_ch.sample_rate,
  146. dai_data->port_config.hdmi_multi_ch.bit_width,
  147. dai_data->channels,
  148. dai_data->port_config.hdmi_multi_ch.channel_allocation,
  149. dai_data->port_config.hdmi_multi_ch.datatype);
  150. return 0;
  151. }
  152. static void msm_dai_q6_hdmi_shutdown(struct snd_pcm_substream *substream,
  153. struct snd_soc_dai *dai)
  154. {
  155. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  156. int rc = 0;
  157. if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  158. pr_info("%s: afe port not started. dai_data->status_mask = %ld\n",
  159. __func__, *dai_data->status_mask);
  160. return;
  161. }
  162. rc = afe_close(dai->id); /* can block */
  163. if (IS_ERR_VALUE(rc))
  164. dev_err(dai->dev, "fail to close AFE port\n");
  165. pr_debug("%s: dai_data->status_mask = %ld\n", __func__,
  166. *dai_data->status_mask);
  167. clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
  168. }
  169. static int msm_dai_q6_hdmi_prepare(struct snd_pcm_substream *substream,
  170. struct snd_soc_dai *dai)
  171. {
  172. struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
  173. int rc = 0;
  174. if (hdmi_ca.set_ca)
  175. dai_data->port_config.hdmi_multi_ch.channel_allocation =
  176. hdmi_ca.ca;
  177. if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  178. rc = afe_port_start(dai->id, &dai_data->port_config,
  179. dai_data->rate);
  180. if (IS_ERR_VALUE(rc))
  181. dev_err(dai->dev, "fail to open AFE port %x\n",
  182. dai->id);
  183. else
  184. set_bit(STATUS_PORT_STARTED,
  185. dai_data->status_mask);
  186. }
  187. return rc;
  188. }
  189. static int msm_dai_q6_hdmi_dai_probe(struct snd_soc_dai *dai)
  190. {
  191. struct msm_dai_q6_hdmi_dai_data *dai_data;
  192. const struct snd_kcontrol_new *kcontrol;
  193. int rc = 0;
  194. dai_data = kzalloc(sizeof(struct msm_dai_q6_hdmi_dai_data),
  195. GFP_KERNEL);
  196. if (!dai_data) {
  197. dev_err(dai->dev, "DAI-%d: fail to allocate dai data\n",
  198. dai->id);
  199. rc = -ENOMEM;
  200. } else
  201. dev_set_drvdata(dai->dev, dai_data);
  202. kcontrol = &hdmi_config_controls[0];
  203. rc = snd_ctl_add(dai->card->snd_card,
  204. snd_ctl_new1(kcontrol, dai_data));
  205. kcontrol = &hdmi_config_controls[1];
  206. rc = snd_ctl_add(dai->card->snd_card,
  207. snd_ctl_new1(kcontrol, dai_data));
  208. return rc;
  209. }
  210. static int msm_dai_q6_hdmi_dai_remove(struct snd_soc_dai *dai)
  211. {
  212. struct msm_dai_q6_hdmi_dai_data *dai_data;
  213. int rc;
  214. dai_data = dev_get_drvdata(dai->dev);
  215. /* If AFE port is still up, close it */
  216. if (test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
  217. rc = afe_close(dai->id); /* can block */
  218. if (IS_ERR_VALUE(rc))
  219. dev_err(dai->dev, "fail to close AFE port\n");
  220. clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
  221. }
  222. kfree(dai_data);
  223. snd_soc_unregister_dai(dai->dev);
  224. return 0;
  225. }
  226. static struct snd_soc_dai_ops msm_dai_q6_hdmi_ops = {
  227. .prepare = msm_dai_q6_hdmi_prepare,
  228. .hw_params = msm_dai_q6_hdmi_hw_params,
  229. .shutdown = msm_dai_q6_hdmi_shutdown,
  230. };
  231. static struct snd_soc_dai_driver msm_dai_q6_hdmi_hdmi_rx_dai = {
  232. .playback = {
  233. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  234. SNDRV_PCM_RATE_192000,
  235. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  236. .channels_min = 2,
  237. .channels_max = 8,
  238. .rate_max = 192000,
  239. .rate_min = 48000,
  240. },
  241. .ops = &msm_dai_q6_hdmi_ops,
  242. .probe = msm_dai_q6_hdmi_dai_probe,
  243. .remove = msm_dai_q6_hdmi_dai_remove,
  244. };
  245. /* To do: change to register DAIs as batch */
  246. static __devinit int msm_dai_q6_hdmi_dev_probe(struct platform_device *pdev)
  247. {
  248. int rc, id;
  249. const char *q6_dev_id = "qcom,msm-dai-q6-dev-id";
  250. rc = of_property_read_u32(pdev->dev.of_node, q6_dev_id, &id);
  251. if (rc) {
  252. dev_err(&pdev->dev,
  253. "%s: missing %s in dt node\n", __func__, q6_dev_id);
  254. return rc;
  255. }
  256. pdev->id = id;
  257. dev_set_name(&pdev->dev, "%s.%d", "msm-dai-q6-hdmi", id);
  258. pr_debug("%s: dev name %s, id:%d\n", __func__,
  259. dev_name(&pdev->dev), pdev->id);
  260. switch (pdev->id) {
  261. case HDMI_RX:
  262. rc = snd_soc_register_dai(&pdev->dev,
  263. &msm_dai_q6_hdmi_hdmi_rx_dai);
  264. break;
  265. default:
  266. dev_err(&pdev->dev, "invalid device ID %d\n", pdev->id);
  267. rc = -ENODEV;
  268. break;
  269. }
  270. return rc;
  271. }
  272. static __devexit int msm_dai_q6_hdmi_dev_remove(struct platform_device *pdev)
  273. {
  274. snd_soc_unregister_dai(&pdev->dev);
  275. return 0;
  276. }
  277. static const struct of_device_id msm_dai_q6_hdmi_dt_match[] = {
  278. {.compatible = "qcom,msm-dai-q6-hdmi"},
  279. {}
  280. };
  281. MODULE_DEVICE_TABLE(of, msm_dai_q6_hdmi_dt_match);
  282. static struct platform_driver msm_dai_q6_hdmi_driver = {
  283. .probe = msm_dai_q6_hdmi_dev_probe,
  284. .remove = msm_dai_q6_hdmi_dev_remove,
  285. .driver = {
  286. .name = "msm-dai-q6-hdmi",
  287. .owner = THIS_MODULE,
  288. .of_match_table = msm_dai_q6_hdmi_dt_match,
  289. },
  290. };
  291. static int __init msm_dai_q6_hdmi_init(void)
  292. {
  293. return platform_driver_register(&msm_dai_q6_hdmi_driver);
  294. }
  295. module_init(msm_dai_q6_hdmi_init);
  296. static void __exit msm_dai_q6_hdmi_exit(void)
  297. {
  298. platform_driver_unregister(&msm_dai_q6_hdmi_driver);
  299. }
  300. module_exit(msm_dai_q6_hdmi_exit);
  301. /* Module information */
  302. MODULE_DESCRIPTION("MSM DSP HDMI DAI driver");
  303. MODULE_LICENSE("GPL v2");