omap-hdmi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * omap-hdmi.c
  3. *
  4. * OMAP ALSA SoC DAI driver for HDMI audio on OMAP4 processors.
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
  6. * Authors: Jorge Candelaria <jorge.candelaria@ti.com>
  7. * Ricardo Neri <ricardo.neri@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/initval.h>
  31. #include <sound/soc.h>
  32. #include <sound/asound.h>
  33. #include <sound/asoundef.h>
  34. #include <video/omapdss.h>
  35. #include <plat/dma.h>
  36. #include "omap-pcm.h"
  37. #include "omap-hdmi.h"
  38. #define DRV_NAME "omap-hdmi-audio-dai"
  39. struct hdmi_priv {
  40. struct omap_pcm_dma_data dma_params;
  41. struct omap_dss_audio dss_audio;
  42. struct snd_aes_iec958 iec;
  43. struct snd_cea_861_aud_if cea;
  44. struct omap_dss_device *dssdev;
  45. };
  46. static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
  47. struct snd_soc_dai *dai)
  48. {
  49. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  50. int err;
  51. /*
  52. * Make sure that the period bytes are multiple of the DMA packet size.
  53. * Largest packet size we use is 32 32-bit words = 128 bytes
  54. */
  55. err = snd_pcm_hw_constraint_step(substream->runtime, 0,
  56. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
  57. if (err < 0) {
  58. dev_err(dai->dev, "could not apply constraint\n");
  59. return err;
  60. }
  61. if (!priv->dssdev->driver->audio_supported(priv->dssdev)) {
  62. dev_err(dai->dev, "audio not supported\n");
  63. return -ENODEV;
  64. }
  65. return 0;
  66. }
  67. static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
  68. struct snd_soc_dai *dai)
  69. {
  70. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  71. return priv->dssdev->driver->audio_enable(priv->dssdev);
  72. }
  73. static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct snd_soc_dai *dai)
  76. {
  77. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  78. struct snd_aes_iec958 *iec = &priv->iec;
  79. struct snd_cea_861_aud_if *cea = &priv->cea;
  80. int err = 0;
  81. switch (params_format(params)) {
  82. case SNDRV_PCM_FORMAT_S16_LE:
  83. priv->dma_params.packet_size = 16;
  84. break;
  85. case SNDRV_PCM_FORMAT_S24_LE:
  86. priv->dma_params.packet_size = 32;
  87. break;
  88. default:
  89. dev_err(dai->dev, "format not supported!\n");
  90. return -EINVAL;
  91. }
  92. priv->dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
  93. snd_soc_dai_set_dma_data(dai, substream,
  94. &priv->dma_params);
  95. /*
  96. * fill the IEC-60958 channel status word
  97. */
  98. /* specify IEC-60958-3 (commercial use) */
  99. iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
  100. /* specify that the audio is LPCM*/
  101. iec->status[0] &= ~IEC958_AES0_NONAUDIO;
  102. iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  103. iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
  104. iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
  105. iec->status[1] = IEC958_AES1_CON_GENERAL;
  106. iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
  107. iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
  108. switch (params_rate(params)) {
  109. case 32000:
  110. iec->status[3] |= IEC958_AES3_CON_FS_32000;
  111. break;
  112. case 44100:
  113. iec->status[3] |= IEC958_AES3_CON_FS_44100;
  114. break;
  115. case 48000:
  116. iec->status[3] |= IEC958_AES3_CON_FS_48000;
  117. break;
  118. case 88200:
  119. iec->status[3] |= IEC958_AES3_CON_FS_88200;
  120. break;
  121. case 96000:
  122. iec->status[3] |= IEC958_AES3_CON_FS_96000;
  123. break;
  124. case 176400:
  125. iec->status[3] |= IEC958_AES3_CON_FS_176400;
  126. break;
  127. case 192000:
  128. iec->status[3] |= IEC958_AES3_CON_FS_192000;
  129. break;
  130. default:
  131. dev_err(dai->dev, "rate not supported!\n");
  132. return -EINVAL;
  133. }
  134. /* specify the clock accuracy */
  135. iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
  136. /*
  137. * specify the word length. The same word length value can mean
  138. * two different lengths. Hence, we need to specify the maximum
  139. * word length as well.
  140. */
  141. switch (params_format(params)) {
  142. case SNDRV_PCM_FORMAT_S16_LE:
  143. iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
  144. iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
  145. break;
  146. case SNDRV_PCM_FORMAT_S24_LE:
  147. iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
  148. iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
  149. break;
  150. default:
  151. dev_err(dai->dev, "format not supported!\n");
  152. return -EINVAL;
  153. }
  154. /*
  155. * Fill the CEA-861 audio infoframe (see spec for details)
  156. */
  157. cea->db1_ct_cc = (params_channels(params) - 1)
  158. & CEA861_AUDIO_INFOFRAME_DB1CC;
  159. cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
  160. cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
  161. cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
  162. cea->db3 = 0; /* not used, all zeros */
  163. /*
  164. * The OMAP HDMI IP requires to use the 8-channel channel code when
  165. * transmitting more than two channels.
  166. */
  167. if (params_channels(params) == 2)
  168. cea->db4_ca = 0x0;
  169. else
  170. cea->db4_ca = 0x13;
  171. cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
  172. /* the expression is trivial but makes clear what we are doing */
  173. cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
  174. priv->dss_audio.iec = iec;
  175. priv->dss_audio.cea = cea;
  176. err = priv->dssdev->driver->audio_config(priv->dssdev,
  177. &priv->dss_audio);
  178. return err;
  179. }
  180. static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  181. struct snd_soc_dai *dai)
  182. {
  183. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  184. int err = 0;
  185. switch (cmd) {
  186. case SNDRV_PCM_TRIGGER_START:
  187. case SNDRV_PCM_TRIGGER_RESUME:
  188. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  189. err = priv->dssdev->driver->audio_start(priv->dssdev);
  190. break;
  191. case SNDRV_PCM_TRIGGER_STOP:
  192. case SNDRV_PCM_TRIGGER_SUSPEND:
  193. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  194. priv->dssdev->driver->audio_stop(priv->dssdev);
  195. break;
  196. default:
  197. err = -EINVAL;
  198. }
  199. return err;
  200. }
  201. static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
  202. struct snd_soc_dai *dai)
  203. {
  204. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  205. priv->dssdev->driver->audio_disable(priv->dssdev);
  206. }
  207. static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
  208. .startup = omap_hdmi_dai_startup,
  209. .hw_params = omap_hdmi_dai_hw_params,
  210. .prepare = omap_hdmi_dai_prepare,
  211. .trigger = omap_hdmi_dai_trigger,
  212. .shutdown = omap_hdmi_dai_shutdown,
  213. };
  214. static struct snd_soc_dai_driver omap_hdmi_dai = {
  215. .playback = {
  216. .channels_min = 2,
  217. .channels_max = 8,
  218. .rates = OMAP_HDMI_RATES,
  219. .formats = OMAP_HDMI_FORMATS,
  220. },
  221. .ops = &omap_hdmi_dai_ops,
  222. };
  223. static __devinit int omap_hdmi_probe(struct platform_device *pdev)
  224. {
  225. int ret;
  226. struct resource *hdmi_rsrc;
  227. struct hdmi_priv *hdmi_data;
  228. bool hdmi_dev_found = false;
  229. hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
  230. if (hdmi_data == NULL) {
  231. dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
  232. return -ENOMEM;
  233. }
  234. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. if (!hdmi_rsrc) {
  236. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM HDMI\n");
  237. return -ENODEV;
  238. }
  239. hdmi_data->dma_params.port_addr = hdmi_rsrc->start
  240. + OMAP_HDMI_AUDIO_DMA_PORT;
  241. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  242. if (!hdmi_rsrc) {
  243. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
  244. return -ENODEV;
  245. }
  246. hdmi_data->dma_params.dma_req = hdmi_rsrc->start;
  247. hdmi_data->dma_params.name = "HDMI playback";
  248. hdmi_data->dma_params.sync_mode = OMAP_DMA_SYNC_PACKET;
  249. /*
  250. * TODO: We assume that there is only one DSS HDMI device. Future
  251. * OMAP implementations may support more than one HDMI devices and
  252. * we should provided separate audio support for all of them.
  253. */
  254. /* Find an HDMI device. */
  255. for_each_dss_dev(hdmi_data->dssdev) {
  256. omap_dss_get_device(hdmi_data->dssdev);
  257. if (!hdmi_data->dssdev->driver) {
  258. omap_dss_put_device(hdmi_data->dssdev);
  259. continue;
  260. }
  261. if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
  262. hdmi_dev_found = true;
  263. break;
  264. }
  265. }
  266. if (!hdmi_dev_found) {
  267. dev_err(&pdev->dev, "no driver for HDMI display found\n");
  268. return -ENODEV;
  269. }
  270. dev_set_drvdata(&pdev->dev, hdmi_data);
  271. ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
  272. return ret;
  273. }
  274. static int __devexit omap_hdmi_remove(struct platform_device *pdev)
  275. {
  276. struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
  277. snd_soc_unregister_dai(&pdev->dev);
  278. if (hdmi_data == NULL) {
  279. dev_err(&pdev->dev, "cannot obtain HDMi data\n");
  280. return -ENODEV;
  281. }
  282. omap_dss_put_device(hdmi_data->dssdev);
  283. return 0;
  284. }
  285. static struct platform_driver hdmi_dai_driver = {
  286. .driver = {
  287. .name = DRV_NAME,
  288. .owner = THIS_MODULE,
  289. },
  290. .probe = omap_hdmi_probe,
  291. .remove = __devexit_p(omap_hdmi_remove),
  292. };
  293. module_platform_driver(hdmi_dai_driver);
  294. MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
  295. MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
  296. MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
  297. MODULE_LICENSE("GPL");
  298. MODULE_ALIAS("platform:" DRV_NAME);