omap-hdmi-audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * omap-hdmi-audio.c -- OMAP4+ DSS HDMI audio support library
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Author: Jyri Sarha <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/err.h>
  21. #include <linux/string.h>
  22. #include <linux/platform_device.h>
  23. #include <sound/soc.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/dmaengine_pcm.h>
  26. #include <uapi/sound/asound.h>
  27. #include <sound/asoundef.h>
  28. #include <sound/omap-pcm.h>
  29. #include <sound/omap-hdmi-audio.h>
  30. #define DRV_NAME "omap-hdmi-audio"
  31. struct hdmi_audio_data {
  32. struct snd_soc_card *card;
  33. const struct omap_hdmi_audio_ops *ops;
  34. struct device *dssdev;
  35. struct snd_dmaengine_dai_dma_data dma_data;
  36. struct omap_dss_audio dss_audio;
  37. struct snd_aes_iec958 iec;
  38. struct snd_cea_861_aud_if cea;
  39. struct mutex current_stream_lock;
  40. struct snd_pcm_substream *current_stream;
  41. };
  42. static
  43. struct hdmi_audio_data *card_drvdata_substream(struct snd_pcm_substream *ss)
  44. {
  45. struct snd_soc_pcm_runtime *rtd = ss->private_data;
  46. return snd_soc_card_get_drvdata(rtd->card);
  47. }
  48. static void hdmi_dai_abort(struct device *dev)
  49. {
  50. struct hdmi_audio_data *ad = dev_get_drvdata(dev);
  51. mutex_lock(&ad->current_stream_lock);
  52. if (ad->current_stream && ad->current_stream->runtime &&
  53. snd_pcm_running(ad->current_stream)) {
  54. dev_err(dev, "HDMI display disabled, aborting playback\n");
  55. snd_pcm_stream_lock_irq(ad->current_stream);
  56. snd_pcm_stop(ad->current_stream, SNDRV_PCM_STATE_DISCONNECTED);
  57. snd_pcm_stream_unlock_irq(ad->current_stream);
  58. }
  59. mutex_unlock(&ad->current_stream_lock);
  60. }
  61. static int hdmi_dai_startup(struct snd_pcm_substream *substream,
  62. struct snd_soc_dai *dai)
  63. {
  64. struct hdmi_audio_data *ad = card_drvdata_substream(substream);
  65. int ret;
  66. /*
  67. * Make sure that the period bytes are multiple of the DMA packet size.
  68. * Largest packet size we use is 32 32-bit words = 128 bytes
  69. */
  70. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  71. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
  72. if (ret < 0) {
  73. dev_err(dai->dev, "Could not apply period constraint: %d\n",
  74. ret);
  75. return ret;
  76. }
  77. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  78. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 128);
  79. if (ret < 0) {
  80. dev_err(dai->dev, "Could not apply buffer constraint: %d\n",
  81. ret);
  82. return ret;
  83. }
  84. snd_soc_dai_set_dma_data(dai, substream, &ad->dma_data);
  85. mutex_lock(&ad->current_stream_lock);
  86. ad->current_stream = substream;
  87. mutex_unlock(&ad->current_stream_lock);
  88. ret = ad->ops->audio_startup(ad->dssdev, hdmi_dai_abort);
  89. if (ret) {
  90. mutex_lock(&ad->current_stream_lock);
  91. ad->current_stream = NULL;
  92. mutex_unlock(&ad->current_stream_lock);
  93. }
  94. return ret;
  95. }
  96. static int hdmi_dai_hw_params(struct snd_pcm_substream *substream,
  97. struct snd_pcm_hw_params *params,
  98. struct snd_soc_dai *dai)
  99. {
  100. struct hdmi_audio_data *ad = card_drvdata_substream(substream);
  101. struct snd_aes_iec958 *iec = &ad->iec;
  102. struct snd_cea_861_aud_if *cea = &ad->cea;
  103. WARN_ON(ad->current_stream != substream);
  104. switch (params_format(params)) {
  105. case SNDRV_PCM_FORMAT_S16_LE:
  106. ad->dma_data.maxburst = 16;
  107. break;
  108. case SNDRV_PCM_FORMAT_S24_LE:
  109. ad->dma_data.maxburst = 32;
  110. break;
  111. default:
  112. dev_err(dai->dev, "format not supported!\n");
  113. return -EINVAL;
  114. }
  115. ad->dss_audio.iec = iec;
  116. ad->dss_audio.cea = cea;
  117. /*
  118. * fill the IEC-60958 channel status word
  119. */
  120. /* initialize the word bytes */
  121. memset(iec->status, 0, sizeof(iec->status));
  122. /* specify IEC-60958-3 (commercial use) */
  123. iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
  124. /* specify that the audio is LPCM*/
  125. iec->status[0] &= ~IEC958_AES0_NONAUDIO;
  126. iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  127. iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
  128. iec->status[1] = IEC958_AES1_CON_GENERAL;
  129. iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
  130. iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
  131. switch (params_rate(params)) {
  132. case 32000:
  133. iec->status[3] |= IEC958_AES3_CON_FS_32000;
  134. break;
  135. case 44100:
  136. iec->status[3] |= IEC958_AES3_CON_FS_44100;
  137. break;
  138. case 48000:
  139. iec->status[3] |= IEC958_AES3_CON_FS_48000;
  140. break;
  141. case 88200:
  142. iec->status[3] |= IEC958_AES3_CON_FS_88200;
  143. break;
  144. case 96000:
  145. iec->status[3] |= IEC958_AES3_CON_FS_96000;
  146. break;
  147. case 176400:
  148. iec->status[3] |= IEC958_AES3_CON_FS_176400;
  149. break;
  150. case 192000:
  151. iec->status[3] |= IEC958_AES3_CON_FS_192000;
  152. break;
  153. default:
  154. dev_err(dai->dev, "rate not supported!\n");
  155. return -EINVAL;
  156. }
  157. /* specify the clock accuracy */
  158. iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
  159. /*
  160. * specify the word length. The same word length value can mean
  161. * two different lengths. Hence, we need to specify the maximum
  162. * word length as well.
  163. */
  164. switch (params_format(params)) {
  165. case SNDRV_PCM_FORMAT_S16_LE:
  166. iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
  167. iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
  168. break;
  169. case SNDRV_PCM_FORMAT_S24_LE:
  170. iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
  171. iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
  172. break;
  173. default:
  174. dev_err(dai->dev, "format not supported!\n");
  175. return -EINVAL;
  176. }
  177. /*
  178. * Fill the CEA-861 audio infoframe (see spec for details)
  179. */
  180. cea->db1_ct_cc = (params_channels(params) - 1)
  181. & CEA861_AUDIO_INFOFRAME_DB1CC;
  182. cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
  183. cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
  184. cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
  185. cea->db3 = 0; /* not used, all zeros */
  186. if (params_channels(params) == 2)
  187. cea->db4_ca = 0x0;
  188. else if (params_channels(params) == 6)
  189. cea->db4_ca = 0xb;
  190. else
  191. cea->db4_ca = 0x13;
  192. if (cea->db4_ca == 0x00)
  193. cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PERMITTED;
  194. else
  195. cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
  196. /* the expression is trivial but makes clear what we are doing */
  197. cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
  198. return ad->ops->audio_config(ad->dssdev, &ad->dss_audio);
  199. }
  200. static int hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  201. struct snd_soc_dai *dai)
  202. {
  203. struct hdmi_audio_data *ad = card_drvdata_substream(substream);
  204. int err = 0;
  205. WARN_ON(ad->current_stream != substream);
  206. switch (cmd) {
  207. case SNDRV_PCM_TRIGGER_START:
  208. case SNDRV_PCM_TRIGGER_RESUME:
  209. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  210. err = ad->ops->audio_start(ad->dssdev);
  211. break;
  212. case SNDRV_PCM_TRIGGER_STOP:
  213. case SNDRV_PCM_TRIGGER_SUSPEND:
  214. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  215. ad->ops->audio_stop(ad->dssdev);
  216. break;
  217. default:
  218. err = -EINVAL;
  219. }
  220. return err;
  221. }
  222. static void hdmi_dai_shutdown(struct snd_pcm_substream *substream,
  223. struct snd_soc_dai *dai)
  224. {
  225. struct hdmi_audio_data *ad = card_drvdata_substream(substream);
  226. WARN_ON(ad->current_stream != substream);
  227. ad->ops->audio_shutdown(ad->dssdev);
  228. mutex_lock(&ad->current_stream_lock);
  229. ad->current_stream = NULL;
  230. mutex_unlock(&ad->current_stream_lock);
  231. }
  232. static const struct snd_soc_dai_ops hdmi_dai_ops = {
  233. .startup = hdmi_dai_startup,
  234. .hw_params = hdmi_dai_hw_params,
  235. .trigger = hdmi_dai_trigger,
  236. .shutdown = hdmi_dai_shutdown,
  237. };
  238. static const struct snd_soc_component_driver omap_hdmi_component = {
  239. .name = "omapdss_hdmi",
  240. };
  241. static struct snd_soc_dai_driver omap5_hdmi_dai = {
  242. .name = "omap5-hdmi-dai",
  243. .playback = {
  244. .channels_min = 2,
  245. .channels_max = 8,
  246. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  247. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  248. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  249. SNDRV_PCM_RATE_192000),
  250. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  251. },
  252. .ops = &hdmi_dai_ops,
  253. };
  254. static struct snd_soc_dai_driver omap4_hdmi_dai = {
  255. .name = "omap4-hdmi-dai",
  256. .playback = {
  257. .channels_min = 2,
  258. .channels_max = 8,
  259. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  260. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  261. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  262. SNDRV_PCM_RATE_192000),
  263. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  264. },
  265. .ops = &hdmi_dai_ops,
  266. };
  267. static int omap_hdmi_audio_probe(struct platform_device *pdev)
  268. {
  269. struct omap_hdmi_audio_pdata *ha = pdev->dev.platform_data;
  270. struct device *dev = &pdev->dev;
  271. struct hdmi_audio_data *ad;
  272. struct snd_soc_dai_driver *dai_drv;
  273. struct snd_soc_card *card;
  274. int ret;
  275. if (!ha) {
  276. dev_err(dev, "No platform data\n");
  277. return -EINVAL;
  278. }
  279. ad = devm_kzalloc(dev, sizeof(*ad), GFP_KERNEL);
  280. if (!ad)
  281. return -ENOMEM;
  282. ad->dssdev = ha->dev;
  283. ad->ops = ha->ops;
  284. ad->dma_data.addr = ha->audio_dma_addr;
  285. ad->dma_data.filter_data = "audio_tx";
  286. ad->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  287. mutex_init(&ad->current_stream_lock);
  288. switch (ha->dss_version) {
  289. case OMAPDSS_VER_OMAP4430_ES1:
  290. case OMAPDSS_VER_OMAP4430_ES2:
  291. case OMAPDSS_VER_OMAP4:
  292. dai_drv = &omap4_hdmi_dai;
  293. break;
  294. case OMAPDSS_VER_OMAP5:
  295. case OMAPDSS_VER_DRA7xx:
  296. dai_drv = &omap5_hdmi_dai;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. ret = snd_soc_register_component(ad->dssdev, &omap_hdmi_component,
  302. dai_drv, 1);
  303. if (ret)
  304. return ret;
  305. ret = omap_pcm_platform_register(ad->dssdev);
  306. if (ret)
  307. return ret;
  308. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  309. if (!card)
  310. return -ENOMEM;
  311. card->name = devm_kasprintf(dev, GFP_KERNEL,
  312. "HDMI %s", dev_name(ad->dssdev));
  313. card->owner = THIS_MODULE;
  314. card->dai_link =
  315. devm_kzalloc(dev, sizeof(*(card->dai_link)), GFP_KERNEL);
  316. if (!card->dai_link)
  317. return -ENOMEM;
  318. card->dai_link->name = card->name;
  319. card->dai_link->stream_name = card->name;
  320. card->dai_link->cpu_dai_name = dev_name(ad->dssdev);
  321. card->dai_link->platform_name = dev_name(ad->dssdev);
  322. card->dai_link->codec_name = "snd-soc-dummy";
  323. card->dai_link->codec_dai_name = "snd-soc-dummy-dai";
  324. card->num_links = 1;
  325. card->dev = dev;
  326. ret = snd_soc_register_card(card);
  327. if (ret) {
  328. dev_err(dev, "snd_soc_register_card failed (%d)\n", ret);
  329. snd_soc_unregister_component(ad->dssdev);
  330. return ret;
  331. }
  332. ad->card = card;
  333. snd_soc_card_set_drvdata(card, ad);
  334. dev_set_drvdata(dev, ad);
  335. return 0;
  336. }
  337. static int omap_hdmi_audio_remove(struct platform_device *pdev)
  338. {
  339. struct hdmi_audio_data *ad = platform_get_drvdata(pdev);
  340. snd_soc_unregister_card(ad->card);
  341. snd_soc_unregister_component(ad->dssdev);
  342. return 0;
  343. }
  344. static struct platform_driver hdmi_audio_driver = {
  345. .driver = {
  346. .name = DRV_NAME,
  347. },
  348. .probe = omap_hdmi_audio_probe,
  349. .remove = omap_hdmi_audio_remove,
  350. };
  351. module_platform_driver(hdmi_audio_driver);
  352. MODULE_AUTHOR("Jyri Sarha <[email protected]>");
  353. MODULE_DESCRIPTION("OMAP HDMI Audio Driver");
  354. MODULE_LICENSE("GPL");
  355. MODULE_ALIAS("platform:" DRV_NAME);