omap-hdmi-audio.c 11 KB

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