omap-hdmi.c 11 KB

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